diff --git a/docs/fsharp/language-reference/sequences.md b/docs/fsharp/language-reference/sequences.md index ced60c0984008..7c09fd0c66bf4 100644 --- a/docs/fsharp/language-reference/sequences.md +++ b/docs/fsharp/language-reference/sequences.md @@ -114,7 +114,7 @@ You can define infinite sequences by using the [Seq.initInfinite](https://fsharp [!code-fsharp[Main](~/samples/snippets/fsharp/fssequences/snippet13.fs)] -[Seq.unfold](https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-seqmodule.html#unfold) generates a sequence from a computation function that takes a state and transforms it to produce each subsequent element in the sequence. The state is just a value that is used to compute each element, and can change as each element is computed. The second argument to `Seq.unfold` is the initial value that is used to start the sequence. `Seq.unfold` uses an option type for the state, which enables you to terminate the sequence by returning the `None` value. The following code shows two examples of sequences, `seq1` and `fib`, that are generated by an `unfold` operation. The first, `seq1`, is just a simple sequence with numbers up to 20. The second, `fib`, uses `unfold` to compute the Fibonacci sequence. Because each element in the Fibonacci sequence is the sum of the previous two Fibonacci numbers, the state value is a tuple that consists of the previous two numbers in the sequence. The initial value is `(1,1)`, the first two numbers in the sequence. +[Seq.unfold](https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-seqmodule.html#unfold) generates a sequence from a computation function that takes a state and transforms it to produce each subsequent element in the sequence. The state is just a value that is used to compute each element, and can change as each element is computed. The second argument to `Seq.unfold` is the initial value that is used to start the sequence. `Seq.unfold` uses an option type for the state, which enables you to terminate the sequence by returning the `None` value. The following code shows two examples of sequences, `seq1` and `fib`, that are generated by an `unfold` operation. The first, `seq1`, is just a simple sequence with numbers up to 20. The second, `fib`, uses `unfold` to compute the Fibonacci sequence. Because each element in the Fibonacci sequence is the sum of the previous two Fibonacci numbers, the state value is a tuple that consists of the previous two numbers in the sequence. The initial value is `(0,1)`, the first two numbers in the sequence. [!code-fsharp[Main](~/samples/snippets/fsharp/fssequences/snippet14.fs)] @@ -127,7 +127,7 @@ The sequence seq1 contains numbers from 0 to 20. The sequence fib contains Fibonacci numbers. -2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 +0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 ``` The following code is an example that uses many of the sequence module functions described here to generate and compute the values of infinite sequences. The code might take a few minutes to run. diff --git a/samples/snippets/fsharp/fssequences/snippet14.fs b/samples/snippets/fsharp/fssequences/snippet14.fs index 2343818589e25..26af4fd7e528f 100644 --- a/samples/snippets/fsharp/fssequences/snippet14.fs +++ b/samples/snippets/fsharp/fssequences/snippet14.fs @@ -12,12 +12,15 @@ for x in seq1 do printf "%d " x let fib = - (1, 1) // Initial state + (0, 1) |> Seq.unfold (fun state -> - if (snd state > 1000) then + let cur, next = state + if cur < 0 then // overflow None else - Some(fst state + snd state, (snd state, fst state + snd state))) + let next' = cur + next + let state' = next, next' + Some (cur, state') ) printfn "\nThe sequence fib contains Fibonacci numbers." for x in fib do printf "%d " x \ No newline at end of file