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

Accessing Array with slice notation
let arr = [| "sa"; "ro"; "ha"; "ta"; "sh"; |]
printfn "%A" arr.[3..4]
OUTPUT

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








Join the conversation! Your thoughts help the community grow.