F# - Arrays

Arrays are fixed-size, zero-based, mutable collections of consecutive data elements that are all of the same type.
You can create arrays using various syntaxes and ways or by using the functions from the Array module. In this section, we will discuss creating arrays without using the module functions.
There are three syntactical ways of creating arrays without functions −
By listing consecutive values between [| and |] and separated by semicolons.
By putting each element on a separate line, in which case the semicolon separator is optional.
By using sequence expressions.
You can access array elements by using a dot operator (.) and brackets ([ and ]).
The following example demonstrates creating arrays −
//using semicolon separator
let array1 = [| 1; 2; 3; 4; 5; 6 |]
for i in 0 .. array1.Length - 1 do
   printf "%d " array1.[i]
printfn" "

// without semicolon separator
let array2 =
   [|
      1
      2
      3
      4
      5
   |]
for i in 0 .. array2.Length - 1 do
   printf "%d " array2.[i]
printfn" "

//using sequence
let array3 = [| for i in 1 .. 10 -> i * i |]
for i in 0 .. array3.Length - 1 do
   printf "%d " array3.[i]
printfn" "
When you compile and execute the program, it yields the following output −
1 2 3 4 5 6
1 2 3 4 5
1 4 9 16 25 36 49 64 81 100

The library module Microsoft.FSharp.Collections.Array supports operations on one-dimensional arrays.
The following table shows the basic operations on Arrays −
In the following section, we will see the uses of some of these functionalities.
The Array module provides several functions that create an array from scratch.
The Array.empty function creates a new empty array.
The Array.empty function creates a new empty array.
The Array.create function creates an array of a specified size and sets all the elements to given values.
The Array.create function creates an array of a specified size and sets all the elements to given values.
The Array.init function creates an array, given a dimension and a function to generate the elements.
The Array.init function creates an array, given a dimension and a function to generate the elements.
The Array.zeroCreate function creates an array in which all the elements are initialized to the zero value.
The Array.zeroCreate function creates an array in which all the elements are initialized to the zero value.
The Array.copy function creates a new array that contains elements that are copied from an existing array.
The Array.copy function creates a new array that contains elements that are copied from an existing array.
The Array.sub function generates a new array from a subrange of an array.
The Array.sub function generates a new array from a subrange of an array.
The Array.append function creates a new array by combining two existing arrays.
The Array.append function creates a new array by combining two existing arrays.
The Array.choose function selects elements of an array to include in a new array.
The Array.choose function selects elements of an array to include in a new array.
The Array.collect function runs a specified function on each array element of an existing array and then collects the elements generated by the function and combines them into a new array.
The Array.collect function runs a specified function on each array element of an existing array and then collects the elements generated by the function and combines them into a new array.
The Array.concat function takes a sequence of arrays and combines them into a single array.
The Array.concat function takes a sequence of arrays and combines them into a single array.
The Array.filter function takes a Boolean condition function and generates a new array that contains only those elements from the input array for which the condition is true.
The Array.filter function takes a Boolean condition function and generates a new array that contains only those elements from the input array for which the condition is true.
The Array.rev function generates a new array by reversing the order of an existing array.
The Array.rev function generates a new array by reversing the order of an existing array.
The following examples demonstrate these functions −
(* using create and set *)
let array1 = Array.create 10 ""
for i in 0 .. array1.Length - 1 do
   Array.set array1 i (i.ToString())
for i in 0 .. array1.Length - 1 do
   printf "%s " (Array.get array1 i)
printfn " "

(* empty array *)
let array2 = Array.empty
printfn "Length of empty array: %d" array2.Length

let array3 = Array.create 10 7.0
printfn "Float Array: %A" array3

(* using the init and zeroCreate *)
let array4 = Array.init 10 (fun index -> index * index)
printfn "Array of squares: %A" array4

let array5 : float array = Array.zeroCreate 10
let (myZeroArray : float array) = Array.zeroCreate 10
printfn "Float Array: %A" array5
When you compile and execute the program, it yields the following output −
0 1 2 3 4 5 6 7 8 9
Length of empty array: 0
Float Array: [|7.0; 7.0; 7.0; 7.0; 7.0; 7.0; 7.0; 7.0; 7.0; 7.0|]
Array of squares: [|0; 1; 4; 9; 16; 25; 36; 49; 64; 81|]
Float Array: [|0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0|]

(* creating subarray from element 5 *)
(* containing 15 elements thereon *)

let array1 = [| 0 .. 50 |]
let array2 = Array.sub array1 5 15
printfn "Sub Array:"
printfn "%A" array2

(* appending two arrays *)
let array3 = [| 1; 2; 3; 4|]
let array4 = [| 5 .. 9 |]
printfn "Appended Array:"
let array5 = Array.append array3 array4
printfn "%A" array5

(* using the Choose function *)
let array6 = [| 1 .. 20 |]
let array7 = Array.choose (fun elem -> if elem % 3 = 0 then
   Some(float (elem))
      else
   None) array6

printfn "Array with Chosen elements:"
printfn "%A" array7

(*using the Collect function *)
let array8 = [| 2 .. 5 |]
let array9 = Array.collect (fun elem -> [| 0 .. elem - 1 |]) array8
printfn "Array with collected elements:"
printfn "%A" array9
When you compile and execute the program, it yields the following output −
Sub Array:
[|5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19|]
Appended Array:
[|1; 2; 3; 4; 5; 6; 7; 8; 9|]
Array with Chosen elements:
[|3.0; 6.0; 9.0; 12.0; 15.0; 18.0|]
Array with collected elements:
[|0; 1; 0; 1; 2; 0; 1; 2; 3; 0; 1; 2; 3; 4|]

The Array.find function takes a Boolean function and returns the first element for which the function returns true, else raises a KeyNotFoundException.
The Array.findIndex function works similarly except that it returns the index of the element instead of the element itself.
The following example demonstrates this.
Microsoft provides this interesting program example, which finds the first element in the range of a given number that is both a perfect square as well as a perfect cube −
let array1 = [| 2 .. 100 |]
let delta = 1.0e-10
let isPerfectSquare (x:int) =
   let y = sqrt (float x)
   abs(y - round y) < delta

let isPerfectCube (x:int) =
   let y = System.Math.Pow(float x, 1.0/3.0)
   abs(y - round y) < delta

let element = Array.find (fun elem -> isPerfectSquare elem && isPerfectCube elem) array1

let index = Array.findIndex (fun elem -> isPerfectSquare elem && isPerfectCube elem) array1

printfn "The first element that is both a square and a cube is %d and its index is %d." element index
When you compile and execute the program, it yields the following output −
The first element that is both a square and a cube is 64 and its index is 62.

 Print
 Add Notes
 Bookmark this page