| id | stdlib-list |
|---|---|
| title | List Module |
| slug | /stdlib/list |
| Form | Signature | Description |
|---|---|---|
values[index] |
'a list -> int -> 'a option |
Zero-based optional indexer for lists. |
let maybeSecond = [10; 20; 30][1]Returns an empty list.
let xs = List.emptyTransforms each item.
let doubled = [1; 2; 3] |> List.map (fun n -> n * 2)Applies a side-effecting function to each item.
["a"; "b"] |> List.iter Console.writeLineMaps and keeps only Some results.
let parsed = ["1"; "x"; "2"] |> List.choose Int.tryParseMaps each item to a list, then concatenates.
let pairs = [1; 2] |> List.collect (fun n -> [n; n])Returns true if any item matches.
let hasEven = [1; 3; 4] |> List.exists (fun n -> n % 2 = 0)Checks scalar membership (unit, int, float, bool, or string). The current implementation does not compare composite values here; use List.exists (fun item -> item = needle) for records, options, tuples, lists, maps, or union values.
let hasTwo = [1; 2; 3] |> List.contains 2Reverses a list.
let reversed = [1; 2; 3] |> List.revRemoves duplicates while preserving the first occurrence order. Equality is recursive for data values; host-managed values compare their visible fields, excluding opaque payloads.
let unique = [1; 2; 2; 3] |> List.distinctFolds left over a list.
let sum = [1; 2; 3] |> List.fold (fun acc n -> acc + n) 0Keeps only matching items.
let odds = [1; 2; 3; 4] |> List.filter (fun n -> n % 2 = 1)Returns item count.
let n = ["a"; "b"] |> List.lengthReturns first matching element.
let maybeBig = [2; 4; 9] |> List.tryFind (fun n -> n > 5)Returns index of first matching element.
let maybeIndex = ["a"; "bb"; "ccc"] |> List.tryGet (fun s -> String.indexOf "c" s |> Option.isSome)Returns the item at a zero-based index.
Returns None for negative or out-of-range indices.
let maybeSecond = List.tryItem 1 [10; 20; 30]Returns first item if present.
let maybeFirst = [10; 20] |> List.tryHeadReturns all elements except the head. An empty input is an evaluation error; use a head :: tail / [] match when emptiness is expected.
let rest = [1; 2; 3] |> List.tailConcatenates two lists.
let all = List.append [1; 2] [3; 4]Count/size arguments must fit 0..2147483647; chunk sizes must also be positive. Invalid arguments raise script errors. Collection callbacks run in the documented order and may themselves have effects; these helpers are pure when their callbacks are pure.
List.isEmpty : 'a list -> boolTests whether the list is empty.
List.isEmpty (List.init 0 (fun i -> i))
// trueList.forall : ('a -> bool) -> 'a list -> boolTests every item, stopping at the first false predicate result. Empty input returns true.
List.forall (fun x -> x > 0) [1;2]
// trueList.mapi : (int -> 'a -> 'b) -> 'a list -> 'b listMaps items with their zero-based index, visiting each item once in input order.
List.mapi (fun i x -> i + x) [10;20]
// [10;21]List.indexed : 'a list -> ((int * 'a)) listPairs each item with its zero-based index.
List.indexed [4;5]
// [(0, 4);(1, 5)]List.init : int -> (int -> 'a) -> 'a listConstructs a list by calling the initializer with indices 0 through count - 1. Zero makes no calls.
List.init 3 (fun i -> i * 2)
// [0;2;4]List.take : int -> 'a list -> 'a listReturns the first count items. Negative counts and counts exceeding the list length raise an error.
List.take 2 [1;2;3]
// [1;2]List.skip : int -> 'a list -> 'a listDrops the first count items. Negative counts and counts exceeding the list length raise an error.
List.skip 2 [1;2;3]
// [3]List.takeWhile : ('a -> bool) -> 'a list -> 'a listReturns the longest prefix satisfying the predicate. Evaluation stops at the first false result.
List.takeWhile (fun x -> x < 3) [1;2;3;1]
// [1;2]List.skipWhile : ('a -> bool) -> 'a list -> 'a listDrops the longest prefix satisfying the predicate. Evaluation stops at the first false result.
List.skipWhile (fun x -> x < 3) [1;2;3;1]
// [3;1]List.partition : ('a -> bool) -> 'a list -> ('a list * 'a list)Returns (matching, other) lists. Calls the predicate once per item and preserves input order in both outputs.
List.partition (fun x -> x % 2 = 0) [1;2;3;4]
// ([2;4], [1;3])List.zip : 'a list -> 'b list -> (('a * 'b)) listPairs corresponding items. Different list lengths raise an error.
List.zip [1;2] ["a";"b"]
// [(1, "a");(2, "b")]List.unzip : (('a * 'b)) list -> ('a list * 'b list)Splits pairs into two lists, preserving order.
List.unzip [(1,"a");(2,"b")]
// ([1;2], ["a";"b"])List.chunkBySize : int -> 'a list -> 'a list listSplits into consecutive chunks of at most size items. Size must be positive. Empty input produces an empty list.
List.chunkBySize 2 [1;2;3]
// [[1;2];[3]]List.scan : ('d -> 'a -> 'd) -> 'd -> 'a list -> 'd listReturns the initial state followed by every accumulated state. Visits items from left to right.
List.scan (fun s x -> s+x) 0 [1;2;3]
// [0;1;3;6]List.sortWith : ('a -> 'a -> int) -> 'a list -> 'a listPerforms a stable ascending sort using a comparer returning a negative, zero, or positive integer. Use this for composite keys. The comparer must define a consistent order.
List.sortWith (fun a b -> a-b) [3;1;2;1]
// [1;1;2;3]List.sortBy : ('a -> 'c) -> 'a list -> 'a listPerforms a stable ascending sort after computing each projection once in input order. Supported keys are int, float, string, bool, and unit. String ordering is ordinal; float NaN sorts before other floats. Composite keys require List.sortWith.
List.sortBy String.length ["bb";"a";"cc"]
// ["a";"bb";"cc"]List.distinctBy : ('a -> 'c) -> 'a list -> 'a listRetains the first item for each structurally equal projected key. Calls the projection once per item in input order.
List.distinctBy String.length ["a";"b";"cc"]
// ["a";"cc"]List.groupBy : ('a -> 'c) -> 'a list -> (('c * 'a list)) listGroups items by structural equality of projected keys. Groups follow first key occurrence, and items retain input order. Calls the projection once per item.
List.groupBy String.length ["a";"bb";"c"]
// [(1, ["a";"c"]);(2, ["bb"])]List.countBy : ('a -> 'c) -> 'a list -> (('c * int)) listCounts structurally equal projected keys. Results follow first key occurrence. Calls the projection once per item.
List.countBy String.length ["a";"bb";"c"]
// [(1, 2);(2, 1)]