8

I'm trying to imitate Haskell's famous infinite fibonacci list in F# using sequences. Why doesn't the following sequence evaluate as expected? How is it being evaluated?

let rec fibs = lazy (Seq.append 
                        (Seq.ofList [0;1]) 
                        ((Seq.map2 (+) (fibs.Force()) 
                                       (Seq.skip 1 (fibs.Force())))))
is7s
  • 3,500
  • 1
  • 20
  • 41
  • 1
    It should be noted that Haskell lazy lists are different from F# sequences (lazy lists are kept in memory when evaluated while sequences are recomputed when you iterate over them). This makes the Haskell pattern slow (if you rewrite it directly) or ugly (if you add caching for performance). – Tomas Petricek Apr 07 '14 at 14:34
  • @TomasPetricek To clarify: You mean the F#-implementation of the Haskell pattern is slow or ugly, no? (Also, adding `Seq.cache` to @kvb's solution doesn't seem too bad to me?) – Søren Debois Apr 07 '14 at 14:39

4 Answers4

9

The problem is that your code still isn't lazy enough: the arguments to Seq.append are evaluated before the result can be accessed, but evaluating the second argument (Seq.map2 ...) requires evaluating its own arguments, which forces the same lazy value that's being defined. This can be worked around by using the Seq.delay function. You can also forgo the lazy wrapper, and lists are already seqs, so you don't need Seq.ofList:

let rec fibs = 
    Seq.append [0;1]
        (Seq.delay (fun () -> Seq.map2 (+) fibs (Seq.skip 1 fibs)))

However, personally I'd recommend using a sequence expression, which I find to be more pleasant to read (and easier to write correctly):

let rec fibs = seq {
    yield 0
    yield 1
    yield! Seq.map2 (+) fibs (fibs |> Seq.skip 1)
}
kvb
  • 54,864
  • 2
  • 91
  • 133
  • 3
    I think you need `let rec fibs = Seq.cache <| seq { ...` to properly emulate the Haskell original: Haskell is call-by-need and so never re-computes previously computed values of a recursively defined sequence. I don't think you get the first n numbers in O(n) time otherwise. – Søren Debois Apr 07 '14 at 14:27
  • Speaking of which, what is the complexity of the above solution? – Søren Debois Apr 07 '14 at 14:27
  • This is a good answer. I had trouble understanding how the 1st example and the 2nd example were the same though. This comment in the source code I thought was somewhat helpful to understand though: https://github.com/fsharp/fsharp/blob/37a100b7caafde0f4df5a1924c9f65f4a18277a8/src/fsharp/FSharp.Core/seq.fs#L488. – pseudoramble May 05 '16 at 02:09
6

To add to kvb's answer, you can also use Seq.unfold to generate a (lazy) sequence:

let fibs = Seq.unfold (fun (a, b) -> Some(a+b, (b, a+b))) (0, 1)

val fibs : seq<int>

Mau
  • 14,234
  • 2
  • 31
  • 52
3

Yet another way:

let rec fib = seq { yield 0; yield! Seq.scan (+) 1 fib }
Ed'ka
  • 6,595
  • 29
  • 30
2

In addition to @kvb's answer: if you just want lazy and not necessarily the zip trick, you can do this:

let fibs = Seq.unfold (fun (m,n) -> Some (m, (n,n+m))) (0,1)  

This makes Seq.take n fibs run in time O(n).

Søren Debois
  • 5,598
  • 26
  • 48