Fsharp: array

By Xah Lee. Date: . Last updated: .

what is array

array is a mutable, fixed-size, contiguous collection, all elements have the same type.

Create Array

literal syntax

// array literal syntax
let xx = [| 1; 2; 3; 4 |]
printfn "%A" xx
// [|1; 2; 3; 4|]

using range oprator

// range
let xx = [| 1..5 |]
printfn "%A" xx
// [|1; 2; 3; 4; 5|]

using sequence expression

// Array sequence expression
let xx = [| for i in 1..4 -> i + 1 |]
printfn "%A" xx
// [|2; 3; 4; 5|]

Array.create

// create 4 string "x"
let tt = Array.create 4 "x"
printfn "%A" tt
[|"x"; "x"; "x"; "x"|]

Empty array

// Empty array
let xx: int[] = [||]

Array.zeroCreate

// create array of 4 items, each is int 0
let xx : int array = Array.zeroCreate 4
printfn "%A" xx
// [|0; 0; 0; 0|]
// create array of 4 items, each is float 0.0
let xt : float array = Array.zeroCreate 4
printfn "%A" xt
// [|0.0; 0.0; 0.0; 0.0|]

🛑 WARNING: comma separator creates array of tuple

// this is a array of a tuple of 3 integers.
let xx = [| 1, 2, 3 |]
printfn "%A" xx
// [|(1, 2, 3)|]

// array of two tuples, each has 3 elements
let xa = [| 1, 2, 3; 4, 5, 6 |]
printfn "%A" xa
// [|(1, 2, 3); (4, 5, 6)|]

Array length

property Length

let x = [| 1; 2; 3 |]

// get length by property.
// note, capital L
printfn "%A" x.Length
// 3

Array.length

let x = [| 1; 2; 3 |]
let tt =  Array.length x
printfn "%A" tt
// 3

get element

syntax to get element

elements are zero indexed.

let x = [| 1; 2; 3 |]
printfn "%A" x[0]
// 1
printfn "%A" x[1]
// 2
let x = [| 1; 2; 3 |]
// get element, old syntax
printfn "%A" x.[0]
// 1

Array.get

let x =  [| 1; 2; 3 |]
let xy = Array.get x 0
printfn "%A" xy
// 1

Array types and modules

type of arrays is the dotnet type System.Array

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)

ai answer

xtodo

Accessing and Mutating

Common Operations

let doubledArr = Array.map (fun x -> x * 2) arr
let sumArr     = Array.sum arr

// In-place mutation
Array.sortInPlace arr

// Convert between collections
let listFromArr = Array.toList arr
let arrFromList = List.toArray [1; 2; 3]

Performance Comparison

Operation List Array
Random access O(n) O(1)
Prepend (cons) O(1) O(n)
Append O(n) O(n)
Memory overhead Higher (links) Lower (contiguous)
Mutability Immutable Mutable

Working Together

// Tuple containing list and array
let data = ([1; 2; 3], [|4; 5; 6|])

// Function returning tuple of results
let stats (nums: int list) =
    let arr = List.toArray nums
    (List.sum nums, Array.averageBy float arr, List.length nums)

// Using all three
let processData () =
    let lst = [1..5]
    let arr = lst |> List.toArray
    let sum, avg, len = stats lst
    (sum, avg, len, arr)

Summary Table

Feature Tuple List ('T list) Array ('T[])
Size Fixed Variable Fixed
Mutability Immutable Immutable Mutable
Access time O(1) O(n) for indexing O(1)
Memory layout Contiguous Linked nodes Contiguous
Best for Small fixed groups Functional pipelines Performance-critical
Typical module Built-in List Array

Next Steps

Would you like me to expand any section with more examples (recursion, performance benchmarks, JSON serialization, etc.)?

Reference