Range Over Iterators in F#
Our example will demonstrate how to work with iterators and linked lists. Here’s the full source code.
Let’s break down what this example does:
- We define a generic
Element
and List
type. Each element can be linked to the next element creating a linked list. - The
Push
method adds values to the list. - The
All
method returns an F# sequence (seq
) that iterates over all elements. genFib
generates an infinite sequence of Fibonacci numbers.- In the
main
function, we create a list, populate it, and print the items. - We also use the
genFib
function to print Fibonacci numbers until a specific condition is met.
Running this code will produce the following output:
Next example: Errors.