A sequence is a logical series of elements all of one type. It is similar to a list, and both are used to represent an ordered collection of values.
A sequence expression is an expression that evaluates a sequence. Sequence expressions can take several forms. The following example shows the simplest form which specifies a range.
let seq1 = seq { 1 .. 10 }
The above line creates a sequence that contains 10 elements, including endpoints 1 and 10.
You can also specify an increment/decrement between two double periods (..
). The following code creates the sequence of multiples of 2.
let seq2 = seq { 0 .. 2 .. 20 }
Sequence expressions are made up of F# expressions that produce values of the sequence. You can also generate values programmatically.
let seq3 = seq { for i in 1 .. 10 -> i * i }
The ->
operator allows you to specify an expression whose value will become a part of the sequence. You can only use ->
if every part of the code that follows it returns a value.
You can specify the do
keyword with an optional yield
that follows.
let seq4 = seq { for i in 1 .. 10 do yield i * i }
// The 'yield' is implicit and doesn't need to be specified in most cases.
let seq5 = seq { for i in 1 .. 10 do i * i }