Working with List in FSharp


Introduction: List is a collection of related values. It is same as linked list data structure used in other languages. List allows us to link data together to form a chain. List plays an important role in functional programming. It can be an Empty list, List of fixed  values or a list value concatenated to it. The below table shows the primitive constructs for building lists.
                   

Expression Description
[ ] For empty List
[value1;...;valuen] List element
Exp::Exp concatenate element with a List. Two colons (::), pronounced as "cons" here.
[exp .. exp] A range of Integers
[for X in List -> exp] A generated List
exp @ exp Concatenates two Lists

Defining List:
   Empty List: An empty List is represented by a square bracket( [ ] ).

              
Syntax: let listname=[]

            
   Like, let emptylist= [ ]
 
List With Fix Elements:  A simple list is defined with an element in square brackets. The list elements are separated with semicolons.
                   
 Syntax: let simplelist=[element1;element2;elementn]

                        Like,
let simplelist= [1;2;3;4;5 ]
                      let
simplelist=['A';'B';'C';'D';'E'
                      let
simplelist=["AAA";"BBB";"CCC"]
                 Or, We can also put line breaks among list element. Semicolon are optional here.
                        Like,   let simplelist=[
                                                    element1
                                                    element2
                                                    elementn]
                        Example,
let simplelist= [1
                                                         2
                                                         3]

We write the following code

let simplelist1= [1;2;3;4;5 ]
let
simplelist2=[1
                 2
                 3
                 4
                 5]
            
printfn "%A" simplelist1
printfn "%A" simplelist2

The output will look like below:

simple list in f#

List with 'cons' operation: Elements can be concatenated to the front of list by built-in operator ( :: ). This is pronounced as 'cons'. Like, if there is a list as listone with element 2,3,4,5 and we want another list with values 1 and values of listone. Then we use this operator. Like below

let
listone= [2;3;4;5]
let listtwo=1 :: listone

We write the following code to understand it.

let
listone= [2;3;4;5]//first list
let
listtwo=1 :: listone//using 'cons'

printfn "%A" listone
printfn "%A" listtwo

Then run the program by pressing Ctrl+F5, The output will look like below

cons list

Range: We can also define a list with range indication. List elements are defined with assigning lower and upper bounds. We can also add a third element as a step in the square bracket to increment by fixed value. Although it is optional. its syntax is given below

                    let listname=[lower_bound..step..upper_bound]

Now we write the following code

let list1= [1..10 ]
let
list2=[1..2..20]

printfn "%A" list1
printfn "%A" list2

The output will look like below:

range
                   
List With Sequence Expression:
We can also construct a list using sequence expression. We write the following code for creating a list of numbers 1 to 10 using sequence expression.

let list1= [for i in 1..10 -> i ]        
printfn "%A" list1

The output will look like below:

sequence expression

Concatenation of Two Lists: Two lists can be concatenated with the @ operator. For this, List should be of a compatible type. We write the following code for concatenation of two list list1 and list2 as list3.

let list1= [1;2;3;4;5]
let
list2=[6;7;8;9;10];
 

let
list3=list1 @ list2
 
               
printfn "%A" list1
printfn "%A" list2
printfn "%A" list3

Output will look like as below figure

concatenate

Up Next
    Ebook Download
    View all
    Learn
    View all