List<'T> Methods in FSharp


Introduction:

The List<'T> type is found in the System.Collections.Generic namespace. It is Mutable collection type. List is similar to an array except - there is no need to specify the size at the time of declaration. Elements or items are added to or retrieved from a List very fast. It dynamically adjust memory size for elements. Now we write the following code to understand List operations. It has several methods which make it more important. Here I am going to discus some of them.

Syntax for defining List<'T>:

    let name = new List<Type>()

Like,
let lst = new List<string>()

Methods:

Add Method:
The Add method adds an element to the end of List. Write the following code.


open
System.Collections.Generic
let
lst = new List<string>()
 
lst.Add("SATISH")
lst.Add("ALOK")
lst.Add("AMIT")
lst.Add("ANIL")
 

for
item in lst do
printfn "%s" item

Output:

list's add method in f#

AddRange Method:

The AddRange method adds an collection of elements/items to the end of a List. Write the following code

open System.Collections.Generic
let
lst = new List<string>()

let
coll = [|"Mohan";"Pramod";"AMITABH"|]

lst.Add("SATISH")
lst.Add("ALOK")
lst.Add("AMIT")
lst.Add("ANIL")
 
lst.AddRange(coll)
 

for
item in lst do
printfn "%s" item

In the above code I have taken a list named coll and added it to List. We can also add collection of elements in AddRange method as like below code. The output will be same for both code.

open
System.Collections.Generic
let
lst = new List<string>()
 
lst.Add("SATISH")
lst.Add("ALOK")
lst.Add("AMIT")
lst.Add("ANIL")

lst.AddRange([|"Mohan";"Pramod";"AMITABH"|])

for item in lst do
printfn "%s" item


Output:

list's addrange method in f#

Contains Method:

The contains method checks whether the specified value is found or not in the list. It returns a boolean value. Look at the following F# code.


open System.Collections.Generic

let
lst = new List<string>()
 
lst.Add("SATISH")
lst.Add("ALOK")
lst.Add("AMIT")
lst.Add("ANIL")
 

let
aa = lst.Contains("SATISH")
let
bb = lst.Contains("RAJ")
 
printfn "%A" aa
printfn "%A" bb

In the above code I am checking two strings - SATISH and RAJ. First string is element of list. So contains method return True for this and the string RAJ is not in list, so this method returns False value.

Output:

list's contans method in f#

Clear Method:

The clear method removes all the elements from the List. It does not contain any arguments. Write the following code.

open System.Collections.Generic
 

let
lst = new List<string>()

lst.Add("SATISH")
lst.Add("ALOK")
printfn "%A" lst
lst.Clear()

printfn "%A" lst

Output:

list's clear method in f#

IndexOf Method:

The IndexOf method returns the index of the specified element. Indexing starts with zero (0) means zero for first element. If specified element is not found in list then it returns value -1. Write the following code.

open System.Collections.Generic

let
lst = new List<string>()
lst.Add("SATISH")
lst.Add("ALOK")
lst.Add("AMIT")
lst.Add("ANKIT")

let
aa = lst.IndexOf ( "AMIT" )
let
bb = lst.IndexOf("ABHI")

printfn "%d" aa
printfn "%d" bb

Output:

list's indexof method in f#

Insert Method:

The Insert method is used for adding new element at specified index. It takes two argument in the form of .Insert( Index, Element) where index specify the index of new element which is going to add to list and Element specify element itself. We write the following code to understand it better.

open System.Collections.Generic
let
lst = new List<string>()
 
lst.Add("SATISH")
lst.Add("ALOK")
lst.Add("AMIT")
lst.Add("ANKIT")
 
 
lst.Insert(2, "ABHI")

for
aa in lst do
 
printf "%s" aa

Output:

list's insert method in f#

Remove Method:

The remove method removes specified element from list. It return boolean value. It return True when list contains specified element and remove that element from list and false when specified element is not exist in list. If list has same value more times, then this method removes only first specified element from list. Below code demonstrates its all functionality. Write the below code in .fs file.

open System.Collections.Generic
let
lst = new List<string>()
 
lst.Add("SATISH")
lst.Add("PRAMOD")
lst.Add("AMIT")
lst.Add("ANKIT")
lst.Add("SATISH")
 
printfn "%A" lst

printfn "Removing the string SATISH"

let
aa = lst.Remove("SATISH")
printfn "%A" aa

printfn "%A" lst

printfn "Removing the string ALOK"

let
bb = lst.Remove("ALOK")
printfn "%A" bb

Output:

list's remove method in f#

RemoveAt Method:

The RemoveAt method remove an element from list at specified index. The index is counted from 0 to (n-1). 

open System.Collections.Generic
let
lst = new List<string>()
 
lst.Add("SATISH")
lst.Add("PRAMOD")
lst.Add("AMIT")
lst.Add("ANKIT")
lst.Add("SATISH")
 
printfn "%A" lst
lst.RemoveAt(2)
printfn "After Removing An Element At Index 2"

for
item in lst do
printfn "%s" item

Here. I have a list of five elements. I am removing an element of index 2. So, the third element (AMIT) will be removed from list.

Output:

list's removeat method in f#


Similar Articles