Introduction: A sequence represents an ordered series of elements of the same type. Sequences are important when we work
with large amounts of data. It is also known as a sequence expression. It is very
similar to Lists - except only one element of sequence can be exist in memory at a
time. Sequences are represented by seq<T>type, which is an alias name for
System.Collections.Generic.IEnumerable<T>.
Advantages of Sequences:
- We can define an infinite sequence in easily ways.
- It support many functions of List.
- It has several function for extracting subsequences.
- It provide an easy way to define sequence of items.
Syntax for Defining a Sequence
Syntax for defining a sequence is given below.
seq { exp
}
Like, seq { 1..10}
Now we create some sequences using different sequence expressions.
Write
the following code:
// Sequence using Range Expressions
let one= seq { 0
.. 100 }
printfn "%A" one
//Sequence using Range Expressions of characters
let two= seq {'a'..'z'}
printfn "%A" two
//Sequence with increment
let three= seq
{1..2..10}
printfn "%A" three
//sequence with yield keyword
let four= seq {
for i in 1 .. 5
do yield i * i }
printfn "%A" four
//sequence with -> operator
let five= seq {
for i in 1 .. 5
-> i * i }
printfn "%A" five
//Sequence with decrement
let six= seq
{10..-1..1}
printfn "%A" six
Now we run this code by pressing Ctrl+F5 key.
Output
Seq Module Functions
Seq.take: The Set.Take function returns the specified number of elements from a sequence. Write the following code:
let
one= seq {1 .. 10 }
let takenSeq =
Seq.take 2 one
printfn "%A" takenSeq





Join the conversation! Your thoughts help the community grow.