# F# Sequences

In this article, we will take a closer look at F# sequences and how they can be used with examples.

F# Sequences is a data abstraction over a collection, and it allows for efficient, lazy evaluation and composition of data, allowing it to provide fast and efficient access to the data.

## Creating F# Sequences

This syntax is used to define sequences in the following way:

```seq { expr }
For example,
let seq1 = seq { 1 .. 8 }```

Sequences can also be created using ranges and comprehensions, similar to lists.

A sequence expression is an expression which you can use to create sequences from the input data.

You can do these things:

• This can be done by specifying a range of values.
• This can be done by specifying a range along with increments or decrements.
• It is done by using a keyword called yield to produce values that will become part of a sequence after being executed.
• You can do this by using the ‘‘ operator.

## F# Sequences Basic Example

Here is the simple example to understand the concept of sequence:

#### Example:

//Sequences let seq1 = seq { 1 .. 8 } //ascending order and increment printfn "The Sequence: %A" seq1 let seq2 = seq { 1 .. 2 .. 20 } //descending order and decrement printfn "The Sequence: %A" seq2 let seq3 = seq {20 .. -2 .. 0} printfn "The Sequence: %A" seq3 //using yield let seq4 = seq { for a in 1 .. 8 do yield a, a*a, a*a*a } printfn "The Sequence: %A" seq4

And the output will be:

```The Sequence: seq [1; 2; 3; 4; ...]
The Sequence: seq [1; 3; 5; 7; ...]
The Sequence: seq [20; 18; 16; 14; ...]
The Sequence: seq [(1, 1, 1); (2, 4, 8); (3, 9, 27); (4, 16, 64); ...]```

## Prime Number Checker

To check the numbers are prime or not in the sequence:

#### Example:

The above program results as follows:

```1
2
3
5
7
11
13
17
19```

## F# Sequence Operations

Here is a table showing the basic operations that can be performed on a sequence data type:

#### Example:

//Creating sequences let emptySeq = Seq.empty let seq1 = Seq.singleton 12 printfn"The singleton sequence:" printfn "%A " seq1 printfn"The init sequence:" let seq2 = Seq.init 8 (fun n -> n * 2) Seq.iter (fun i -> printf "%d " i) seq2 printfn"" //converting an array to sequence by using cast printfn"The array sequence 1:" let seq3 = [| 1 .. 8 |] :> seq<int> Seq.iter (fun i -> printf "%d " i) seq3 printfn"" //converting an array to sequence by using Seq.ofArray printfn"The array sequence 2:" let seq4 = [| 1..2.. 20 |] |> Seq.ofArray Seq.iter (fun i -> printf "%d " i) seq4 printfn""
The output of the above example is as follows:
```The singleton sequence:seq 
The init sequence:
0 2 4 6 8 10 12 14
The array sequence 1:
1 2 3 4 5 6 7 8
The array sequence 2:
1 3 5 7 9 11 13 15 17 19```

The following points need to be noted:

• The method Seq.empty creates an empty sequence when it is called.
• In the Seq.singleton method, only one element of a sequence is created by specifying its name.
• The method Seq.init creates a sequence of elements within which a function is passed in order to create the elements.
• The Seq.ofArray and Seq.ofList<‘T> methods are used to create sequences from arrays and lists of elements.
• In the Seq.iter method, you can iterate through a sequence by iterating through it.

## Sum and Average Calculation

Calculating the sum and average of all elements in sequence:

#### Example:

let num1 = seq { 2.0..2.0..20.0} let num2 = seq { 2..2.. 20} let sum = Seq.sum num2 let avg = Seq.average num1 printfn "Sum: %d" sum printfn "Average: %f" avg

## F# Sequence Seq.unfold

Using the Seq.unfold method, you can create a sequence from a state that is transformable so that each element in the sequence can be created using that state.

This is done by transforming the state before going on with the computation.

Here are the first 30 natural numbers produced by the Seq.unfold method:

#### Example:

let seq1 = Seq.unfold (fun state -> if (state > 30) then None else Some(state, state + 1)) 0 printfn "The sequence of natural numbers from 0 to 30. \n" for x in seq1 do printf "%d, " x printfn" "

And the result of the above example is:

```The sequence seq1 contains numbers from 0 to 30.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,```

## Array Conversion to Sequence and Sorting

Below is an example of creating an array and then converting it into a sequence, as well as sorting the sequence.

#### Example:

let numbers = [| 5; 1; 4; 2; 3 |] let sequence = Seq.ofArray numbers let sortedNumbers = Seq.sort sequence printfn "Original sequence: %A" numbers printfn "Sorted sequence: %A" sortedNumbers The output of the above example is as follows: Original sequence: [|5; 1; 4; 2; 3|] Sorted sequence: seq [1; 2; 3; 4; …]

## Seq.truncate & Seq.take

This method, Seq.truncate, creates a new sequence from another sequence, but restricts the next element in the new sequence to a specified number.

With the Seq.take method, a new sequence is created which begins with precisely a specified number of elements and contains them starting from the beginning of the sequence.

#### Example:

let sequence = seq { for i in 1 .. 10 -> 2*i } let truncate = Seq.truncate 5 sequence let take = Seq.take 4 sequence printfn"The original: " Seq.iter (fun i -> printf "%d " i) sequence printfn"" printfn"The truncated: " Seq.iter (fun i -> printf "%d " i) truncate printfn"" printfn"The take: " Seq.iter (fun i -> printf "%d " i) take printfn""

The output will be:

```The original: 2 4 6 8 10 12 14 16 18 20
The truncated:
2 4 6 8 10
The take:
2 4 6 8```

### Example Explanation

Above example demonstrates the use of the Seq.truncate and Seq.take functions in F# to obtain subsequences from a sequence.

First, we define a sequence using a sequence expression that generates the values 2, 4, 6, 8, 10, 12, 14, 16, 18, 20.

Then, we use Seq.truncate to truncate the sequence to the first 5 elements and store the result in truncate. We also use Seq.take to take the first 4 elements of the sequence and store the result in.

Finally, we use Seq.iter to print out the original sequence and the two subsequences. The Seq.iter function applies a function to each element of a sequence, and in this case, we use it to print out the elements of the sequence.

Note that Seq.truncate returns a new sequence with the specified number of elements from the beginning of the original sequence. If the original sequence has fewer elements than the specified number, the entire sequence is returned.

Similarly, Seq.take returns a new sequence with the specified number of elements from the beginning of the original sequence. If the original sequence has fewer elements than the specified number, only the available elements are returned.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0 