F# Resources

You are here: main » core » list


|

Meta

Table of Contents

List

F# list type is a singly-linked list.

Examples

Most of these examples also work with arrays and sequences.

// List with 3 elements
> [1; 2; 3];;
val it : int list = [1; 2; 3]

// shorthand
> [1 .. 10];;
val it : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]

> [10 .. -4 .. 1];;
val it : int list = [10; 6; 2]

// add an element on top
> 1::[2; 3];;
val it : int list = [1; 2; 3]

// nth element (first is 0)
> [10..20].[3];;
val it : int = 13

// count the elements
> List.length [1; 4];;
val it : int = 2

// the first matching element
> List.find (fun x -> x % 4 = 0) [2; 5; 8; 1];;
val it : int = 8

// Apply a function on each element
> List.map (fun x -> x * x) [1..10];;
val it : int list = [1; 4; 9; 16; 25; 36; 49; 64; 81; 100]

// Let's do it with 2 lists
> List.map2 (+) [1; 4; 5] [2; 3; 0];;
val it : int list = [3; 7; 5]

// Keep matching elements
> List.filter (fun x -> x % 2 = 0) [1 .. 10];;
val it : int list = [2; 4; 6; 8; 10]

// A bit like filter, but get a couple of lists
> [10 .. 19] |> List.partition (fun x -> x % 3 = 0);;
val it : int list * int list = ([12; 15; 18], [10; 11; 13; 14; 16; 17; 19])

// map + filter
> [10 .. 19] |> List.choose (fun x -> if x % 3 = 0 then Some (x / 3) else None);;
val it : int list = [4; 5; 6]

// map + find
> [10 .. 19] |> List.pick (fun x -> if x % 3 = 0 then Some (x / 3) else None);;
val it : int = 4

// easy
> List.sort [1; 5; 9; 6; 4; 3; 7];;
val it : int list = [1; 3; 4; 5; 6; 7; 9]

// sort using a transformation function
> List.sortBy string [1 .. +4 .. 30];;
val it : int list = [1; 13; 17; 21; 25; 29; 5; 9]

// sort by length, then by string
> ["abc"; "tre"; "zert"; "aaaa"] |> List.sortBy (fun s -> s.Length, s);;
val it : string list = ["abc"; "tre"; "aaaa"; "zert"]

> List.sortWith (fun x y -> compare y x) [1 .. +4 .. 30];;
val it : int list = [29; 25; 21; 17; 13; 9; 5; 1]

> List.replicate 5 "a";;
val it : string list = ["a"; "a"; "a"; "a"; "a"]

// Give the length and an initialization function
> List.init 10 string;;
val it : string list = ["0"; "1"; "2"; "3"; "4"; "5"; "6"; "7"; "8"; "9"]

> List.rev [4; 6; -3; 1];;
val it : int list = [1; -3; 6; 4]

// Apply an accumulator over a sequence (like Aggregate in Linq)
// Here, compute: 100 - 1 - 5 - 6
> List.fold (-) 100 [1; 5; 6];;
val it : int = 88

// The same, in the over way
> List.foldBack (+) ["a"; "b"; "c"] "d";;
val it : string = "abcd"

// Like fold, using head of list as accumulator
> List.reduce (fun acc x -> if x < 3 then acc else acc + x) [1 .. 10];;
val it : int = 53

// Like fold, but return list of intermediate results
> List.scan (+) 0 [1; 4; 3; 5; -2];;
val it : int list = [0; 1; 5; 8; 13; 11]

// Change the order of elements (the function maps the indexes)
> [10 .. 19] |> List.permute (function 0 -> 5 | 5 -> 0 | i -> i);;
val it : int list = [15; 11; 12; 13; 14; 10; 16; 17; 18; 19]

> [10 .. 19] |> List.permute (function 0 -> 9 | i -> i - 1);;
val it : int list = [11; 12; 13; 14; 15; 16; 17; 18; 19; 10]

// called "any" in some languages
> List.exists (fun x -> x % 4 = 0) [2; 5; 8; 1];;
val it : bool = true

// called "all" in some languages
> List.forall (fun x -> x > 0) [2; 5; -1; 3];;
val it : bool = false

// reduce (+)
> List.sum [4; 6; -3; 1];;
val it : int = 8

// map + sum
> [4; 6; -3; 1] |> List.sumBy abs;;
val it : int = 14

// flatten a list of lists
> List.concat [[1; 2]; [5; 6]];;
val it : int list = [1; 2; 5; 6]

// map + concat
> List.collect (fun x -> [1..x]) [2; 5; 3];;
val it : int list = [1; 2; 1; 2; 3; 4; 5; 1; 2; 3]

// combine two lists in list of pairs
> List.zip [1..3] ['f'; 'o'; 'o'];;
val it : (int * char) list = [(1, 'f'); (2, 'o'); (3, 'o')]

// split a list of pairs
> List.unzip [1, 'f'; 2, 'o'; 3, 'o'];;
val it : int list * char list = ([1; 2; 3], ['f'; 'o'; 'o'])

> List.average [2.3; 4.1; 5.3];;
val it : float = 3.9

> List.averageBy float [4; 6; -3; 1];;
val it : float = 2.0

> List.min [5; 2; 3];;
val it : int = 2

> List.minBy abs [-5; 2; -1; 3];;
val it : int = -1

> List.max [5; 2; 3];;
val it : int = 5

> List.maxBy abs [-5; 2; -1; 3];;
val it : int = -5

See also