Working with Array and Function in F#

Array

Array is a collection of similar data elements. The type of all F# arrays is the .NET Framework type Array. Therefore, F# arrays support all the functionality available in Array.

You can create a small array by listing consecutive values between [| and |] and separated by semicolons.

For Example

let arr = [| 4; 5; 6; 7; 8; |]
We can define above array as below array.
let arr = 
[|
4
5
6
7
8
|]

You can also use sequence expressions to create arrays. Following is an example that creates an array of squares of integersfrom 1 to 10.
let arr = [| for j in 1 .. 10 -> j * j |]
Accessing Array 
Array Index start at 0. An Array element is retrieved by a square bracket with the dot(.) operator.
For Example
let arr = [| 4; 5; 6; 7; 8; |]
arr.[0]
arr.[1]
arr.[2]
arr.[3]
Now print the above values with Index.

let arr = [| 4; 5; 6; 7; 8; |]

printfn "%i" arr.[0]

printfn "%i" arr.[1]

printfn "%i" arr.[2]

printfn "%i" arr.[3]

 

OUTPUT


arr1.gif

 

Accessing Array with slice notation

 

let arr = [| "sa"; "ro"; "ha"; "ta"; "sh"; |]

printfn "%A" arr.[3..4]

 

OUTPUT


arr2.gif

 

Simple Functions of Array

 

The below defines functions of Array in F#.

 

Array.Create

 

Array.create is used to create a array. Array.create creates an array of a specified size and sets all the elements to provided values.

 

Length -  Length represent total number of elements in all the dimensions of array.

 

Set Function- Sets an element of an array.

 

Get function- Gets an element from an array.

 

For Example

 

The below example defines all the above functions.

 

let arr = Array.create 20 ""

for j in 0 .. arr.Length - 5 do

    Array.set arr j (j.ToString())

for j in 0 .. arr.Length - 1 do

    printf "%s " (Array.get arr j)

 

OUTPUT


arr3.gif

 

Array.init  Function

 

This function is used to create an array, given a dimension and a function to generate the elements.

 

let Arr = Array.init 5 (fun index -> index * index)

printfn "Array of squares: %A" Arr

 

OUTPUT


arr4.gif

 

Array.append Function

 

This Method is used to combine to array.

 

For Example

 

let arr1 = [| 4; 5; 6; 7; 8; |]

let arr2 = [| 4; 5; 6; 7; 8; |]

printfn " Array3 :%A" (Array.append [| arr1|] [| arr2|])

 

OUTPUT


arr5.gif

 

Array.rev Function

 

This function is used to generate a new array by reversing the order of an existing array.

 

For Example

 

let arr1 =  "rohatash".ToCharArray()

printfn " Array3 :%A" (Array.rev( arr1))

 

OUTPUT


arr6.gif

 

Array.sub Function

 

This function is used to generate a new array from a subarray of an array.

 

For Example

 

let arr1 = [| 0 .. 10 |]

let arr2 = Array.sub arr1 3 5

printfn " Sub Array: %A" arr2

 

OUTPUT


arr7.gif

 

Array.collect Function

 

This function is used to collects the elements generated by the function and combines them into a new array with existing Array.

 

For Example

 

printfn "%A" (Array.collect (fun elem -> [| 0 .. elem |]) [| 10; 15; 20|])

 

OUTPUT


arr8.gif

 

Array.filter Function

 

This function is used to filter element from an array.

 

For Example

 

printfn "%A" (Array.filter (fun fil -> fil % 3 = 0) [| 1 .. 50|])

 

OUTPUT


arr9.gif


Similar Articles