I am trying to create a sequence lazily by using F#.
The sequence is defined as follows:
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
Here is what I have so far but it dosn’t seem to work:
let tri_seq = 1.0 |> Seq.unfold (fun x -> match x with | _ -> Some (x, 0.5*x*(x + 1.0)))
Thank you very much who can help me figure out how unfold works. Thanks
Edit: I marked the first answer as correct but it dosnt work, however I slightly modified it and it worked.
let tri_seq = 1.0 |> Seq.unfold (fun x -> Some (0.5 * x * (x + 1.0),x + 1.0))
First off, why do you use
matchif you’ve got only one case?Second, what “doesn’t seem to work”? Are you aware that you produce an infinite list?
/Edit: For completeness’ sake, here’s the correct solution, which the OP found himself and posted as a comment: