FSharp.Core


List Module

Contains operations for working with values of type list.

Functions and values

Function or value Description

List.allPairs list1 list2

Full Usage: List.allPairs list1 list2

Parameters:
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.

Returns: ('T1 * 'T2) list The resulting list of pairs.

Returns a new list that contains all pairings of elements from the first and second lists.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

Returns: ('T1 * 'T2) list

The resulting list of pairs.

Example

 let people = ["Kirk"; "Spock"; "McCoy"]
 let numbers = [1;2]

 people |> List.allPairs numbers
The sample evaluates to
 [(1, "Kirk"); (1, "Spock"); (1, "McCoy"); (2, "Kirk"); (2, "Spock"); (2, "McCoy")]

List.append list1 list2

Full Usage: List.append list1 list2

Parameters:
    list1 : 'T list - The first input list.
    list2 : 'T list - The second input list.

Returns: 'T list The resulting list.

Returns a new list that contains the elements of the first list followed by elements of the second.

list1 : 'T list

The first input list.

list2 : 'T list

The second input list.

Returns: 'T list

The resulting list.

Example

 List.append [1..3] [4..7]     //  evaluates [1; 2; 3; 4; 5; 6; 7]
 
 [4..7] |> List.append [1..3]  //  evaluates [1; 2; 3; 4; 5; 6; 7]

List.average list

Full Usage: List.average list

Parameters:
    list : ^T list - The input list.

Returns: ^T The resulting average.
Modifiers: inline
Type parameters: ^T

Returns the average of the elements in the list.

Raises ArgumentException if list is empty.

list : ^T list

The input list.

Returns: ^T

The resulting average.

ArgumentException Thrown when the list is empty.
Example

 [1.0 .. 9.0] |> List.average  // evaluates 5.0

Example

 [1 .. 9] |> List.average
The sample does not compile because The type 'int' does not support the operator 'DivideByInt' (see averageBy examples for a solution)

List.averageBy projection list

Full Usage: List.averageBy projection list

Parameters:
    projection : 'T -> ^U - The function to transform the list elements into the type to be averaged.
    list : 'T list - The input list.

Returns: ^U The resulting average.
Modifiers: inline
Type parameters: 'T, ^U

Returns the average of the elements generated by applying the function to each element of the list.

Raises ArgumentException if list is empty.

projection : 'T -> ^U

The function to transform the list elements into the type to be averaged.

list : 'T list

The input list.

Returns: ^U

The resulting average.

ArgumentException Thrown when the list is empty.
Example

Average the age of persons by extracting the age from records

 type People = {
    name: string
    age: int }

 let getAgeAsFloat person = float person.age

 let people = 
     [ { name = "Kirk"; age = 26 }
       { name = "Spock"; age = 90 }
       { name = "McCoy"; age = 37 } ]

 people |> List.averageBy getAgeAsFloat  // evaluates 51.0

Example

Average a list of integer numbers by converting to float

 [1 .. 9] |> List.averageBy float  // evaluates 5.0

List.choose chooser list

Full Usage: List.choose chooser list

Parameters:
    chooser : 'T -> 'U option - The function to generate options from the elements.
    list : 'T list - The input list.

Returns: 'U list The list comprising the values selected from the chooser function.

Applies the given function to each element of the list. Returns the list comprised of the results x for each element where the function returns Some(x)

chooser : 'T -> 'U option

The function to generate options from the elements.

list : 'T list

The input list.

Returns: 'U list

The list comprising the values selected from the chooser function.

Example

 type Happiness =
     | AlwaysHappy
     | MostOfTheTimeGrumpy

 type People =
     {
       Name: string
       Happiness: Happiness
     }

 let takeJustHappyPersons person =
     match person.Happiness with
     | AlwaysHappy -> Some person.Name
     | MostOfTheTimeGrumpy -> None

 let candidatesForTheTrip = 
     [ { Name = "SpongeBob"; Happiness = AlwaysHappy }
       { Name = "Patrick"; Happiness = AlwaysHappy }
       { Name = "Squidward"; Happiness = MostOfTheTimeGrumpy } ]

 candidatesForTheTrip |> List.choose takeJustHappyPersons
The sample evaluates to [ "SpongeBob"; "Patrick" ]

Example

Using the identity function id (is defined like fun x -> x):

 
 let input1 = [ Some 1; None; Some 3; None ]

 input1 |> List.choose id  // evaluates [1; 3]

Example

 let input2: int option list = [] 

 input2 |> List.choose id  // evaluates to the empty list

Example

 let input3: string option list = [None; None]

 input3 |> List.choose id  // evaluates to the empty list

List.chunkBySize chunkSize list

Full Usage: List.chunkBySize chunkSize list

Parameters:
    chunkSize : int - The maximum size of each chunk.
    list : 'T list - The input list.

Returns: 'T list list The list divided into chunks.

Divides the input list into chunks of size at most chunkSize.

chunkSize : int

The maximum size of each chunk.

list : 'T list

The input list.

Returns: 'T list list

The list divided into chunks.

ArgumentException Thrown when chunkSize is not positive.
Example

 [1 .. 10 ] |> List.chunkBySize 3  // evaluates
Evaluates to [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]; [10]] . Please notice the last chunk.
 [1 .. 5 ] |> List.chunkBySize 10
Evaluates to [[1; 2; 3; 4; 5]]
 let input : string list = []

 input |> List.chunkBySize 10
Evaluates to []. The result has type string list list.

List.collect mapping list

Full Usage: List.collect mapping list

Parameters:
    mapping : 'T -> 'U list - The function to transform each input element into a sublist to be concatenated.
    list : 'T list - The input list.

Returns: 'U list The concatenation of the transformed sublists.

For each element of the list, applies the given function. Concatenates all the results and return the combined list.

mapping : 'T -> 'U list

The function to transform each input element into a sublist to be concatenated.

list : 'T list

The input list.

Returns: 'U list

The concatenation of the transformed sublists.

Example

For each positive number in the array we are generating all the previous positive numbers

 [1..4] |> List.collect (fun x -> [1..x])
The sample evaluates to [1; 1; 2; 1; 2; 3; 1; 2; 3; 4] (added extra spaces for easy reading)

List.compareWith comparer list1 list2

Full Usage: List.compareWith comparer list1 list2

Parameters:
    comparer : 'T -> 'T -> int - A function that takes an element from each list and returns an int. If it evaluates to a non-zero value iteration is stopped and that value is returned.
    list1 : 'T list - The first input list.
    list2 : 'T list - The second input list.

Returns: int Returns the first non-zero result from the comparison function. If the first list has a larger element, the return value is always positive. If the second list has a larger element, the return value is always negative. When the elements are equal in the two lists, 1 is returned if the first list is longer, 0 is returned if they are equal in length, and -1 is returned when the second list is longer.
Modifiers: inline
Type parameters: 'T

Compares two lists using the given comparison function, element by element.

comparer : 'T -> 'T -> int

A function that takes an element from each list and returns an int. If it evaluates to a non-zero value iteration is stopped and that value is returned.

list1 : 'T list

The first input list.

list2 : 'T list

The second input list.

Returns: int

Returns the first non-zero result from the comparison function. If the first list has a larger element, the return value is always positive. If the second list has a larger element, the return value is always negative. When the elements are equal in the two lists, 1 is returned if the first list is longer, 0 is returned if they are equal in length, and -1 is returned when the second list is longer.

Example

 let closerToNextDozen a b =
   (a % 12).CompareTo(b % 12)

 let input1 = [1; 10]
 let input2 = [1; 10]

 (input1, input2) ||> List.compareWith closerToNextDozen
Evaluates to 0

Example

 let closerToNextDozen a b =
   (a % 12).CompareTo(b % 12)

 let input1 = [1; 5]
 let input2 = [1; 8]

 (input1, input2) ||> List.compareWith closerToNextDozen
Evaluates to -1

Example

 let closerToNextDozen a b =
   (a % 12).CompareTo(b % 12)

 let input1 = [1; 11]
 let input2 = [1; 13]

 (input1, input2) ||> List.compareWith closerToNextDozen
Evaluates to 1

Example

 let closerToNextDozen a b =
   (a % 12).CompareTo(b % 12)

 let input1 = [1; 2]
 let input2 = [1]

 (input1, input2) ||> List.compareWith closerToNextDozen
Evaluates to 1

Example

 let closerToNextDozen a b =
   (a % 12).CompareTo(b % 12)

 let input1 = [1]
 let input2 = [1; 2]

 (input1, input2) ||> List.compareWith closerToNextDozen
Evaluates to -1

List.concat lists

Full Usage: List.concat lists

Parameters:
    lists : seq<'T list> - The input sequence of lists.

Returns: 'T list The resulting concatenated list.

Returns a new list that contains the elements of each the lists in order.

lists : seq<'T list>

The input sequence of lists.

Returns: 'T list

The resulting concatenated list.

Example

 let input = [ [1;2]
               [3;4;5]
               [6;7;8;9] ]
 input |> List.concat  // evaluates [1; 2; 3; 4; 5; 6; 7; 8; 9]

List.contains value source

Full Usage: List.contains value source

Parameters:
    value : 'T - The value to locate in the input list.
    source : 'T list - The input list.

Returns: bool True if the input list contains the specified element; false otherwise.
Modifiers: inline
Type parameters: 'T

Tests if the list contains the specified element.

value : 'T

The value to locate in the input list.

source : 'T list

The input list.

Returns: bool

True if the input list contains the specified element; false otherwise.

Example

 [1..9] |> List.contains 0
Evaluates to false.

Example

 [1..9] |> List.contains 3
Evaluates to true.

Example

 let input = [1, "SpongeBob"; 2, "Patrick"; 3, "Squidward"; 4, "Mr. Krabs"]

 input |> List.contains (2, "Patrick")
Evaluates to true.

Example

 let input = [1, "SpongeBob"; 2, "Patrick"; 3, "Squidward"; 4, "Mr. Krabs"]

 input |> List.contains (22, "Patrick")
Evaluates to false.

List.countBy projection list

Full Usage: List.countBy projection list

Parameters:
    projection : 'T -> 'Key - A function transforming each item of the input list into a key to be compared against the others.
    list : 'T list - The input list.

Returns: ('Key * int) list The result list.

Applies a key-generating function to each element of a list and returns a list yielding unique keys and their number of occurrences in the original list.

projection : 'T -> 'Key

A function transforming each item of the input list into a key to be compared against the others.

list : 'T list

The input list.

Returns: ('Key * int) list

The result list.

Example

Counting the number of occurrences of chars

 let input = ['H'; 'a'; 'p'; 'p'; 'y']

 input |> List.countBy id
Evaluates [('H', 1); ('a', 1); ('p', 2); ('y', 1)]

List.distinct list

Full Usage: List.distinct list

Parameters:
    list : 'T list - The input list.

Returns: 'T list The result list.

Returns a list that contains no duplicate entries according to generic hash and equality comparisons on the entries. If an element occurs multiple times in the list then the later occurrences are discarded.

list : 'T list

The input list.

Returns: 'T list

The result list.

Example

 let input = [6;1;2;3;1;4;5;5]

 input |> List.distinct
Evaluates to [6; 1; 2; 3; 4; 5].

List.distinctBy projection list

Full Usage: List.distinctBy projection list

Parameters:
    projection : 'T -> 'Key - A function transforming the list items into comparable keys.
    list : 'T list - The input list.

Returns: 'T list The result list.

Returns a list that contains no duplicate entries according to the generic hash and equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the list then the later occurrences are discarded.

projection : 'T -> 'Key

A function transforming the list items into comparable keys.

list : 'T list

The input list.

Returns: 'T list

The result list.

Example

 let isEven x = 0 = x % 2

 let input = [6;1;2;3;1;4;5;5]

 input |> List.distinctBy isEven  // evaluates [6; 1]

List.empty

Full Usage: List.empty

Returns: 'T list

Returns an empty list of the given type.

Returns: 'T list

List.exactlyOne list

Full Usage: List.exactlyOne list

Parameters:
    list : 'T list - The input list.

Returns: 'T The only element of the list.

Returns the only element of the list.

list : 'T list

The input list.

Returns: 'T

The only element of the list.

ArgumentException Thrown when the input does not have precisely one element.
Example

 ["the chosen one"] |> List.exactlyOne // evaluates "the chosen one"

Example

 let input : string list = []

 input |> List.exactlyOne
Will throw the exception: System.ArgumentException: The input sequence was empty

Example

 [1..5] |> List.exactlyOne
Will throw the exception: System.ArgumentException: The input sequence contains more than one element

List.except itemsToExclude list

Full Usage: List.except itemsToExclude list

Parameters:
    itemsToExclude : seq<'T> - A sequence whose elements that also occur in the input list will cause those elements to be removed from the result.
    list : 'T list - A list whose elements that are not also in itemsToExclude will be returned.

Returns: 'T list A list that contains the distinct elements of list that do not appear in itemsToExclude.

Returns a new list with the distinct elements of the input list which do not appear in the itemsToExclude sequence, using generic hash and equality comparisons to compare values.

itemsToExclude : seq<'T>

A sequence whose elements that also occur in the input list will cause those elements to be removed from the result.

list : 'T list

A list whose elements that are not also in itemsToExclude will be returned.

Returns: 'T list

A list that contains the distinct elements of list that do not appear in itemsToExclude.

ArgumentNullException Thrown when itemsToExclude is null.
Example

 let input = [1, "Kirk"; 2, "Spock"; 3, "Kenobi"] 

 input |> List.except [3, "Kenobi"]
Evaluates to [(1, "Kirk"); (2, "Spock")].

Example

 [0..10] |> List.except [1..5]  // evaluates [0; 6; 7; 8; 9; 10]

Example

 [1..5] |> List.except [0..10]  // evaluates []

List.exists predicate list

Full Usage: List.exists predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: bool True if any element satisfies the predicate.

Tests if any element of the list satisfies the given predicate.

The predicate is applied to the elements of the input list. If any application returns true then the overall result is true and no further elements are tested. Otherwise, false is returned.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: bool

True if any element satisfies the predicate.

Example

 let input = [1, "Kirk"; 2, "Spock"; 3, "Kenobi"] 
 
 input |> List.exists (fun x -> x = (3, "Kenobi"))  // evaluates true
 
 input |> List.exists (fun (n, name) -> n > 5)      // evaluates false

List.exists2 predicate list1 list2

Full Usage: List.exists2 predicate list1 list2

Parameters:
    predicate : 'T1 -> 'T2 -> bool - The function to test the input elements.
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.

Returns: bool True if any pair of elements satisfy the predicate.

Tests if any pair of corresponding elements of the lists satisfies the given predicate.

The predicate is applied to matching elements in the two collections up to the lesser of the two lengths of the collections. If any application returns true then the overall result is true and no further elements are tested. Otherwise, if one collections is longer than the other then the ArgumentException exception is raised. Otherwise, false is returned.

predicate : 'T1 -> 'T2 -> bool

The function to test the input elements.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

Returns: bool

True if any pair of elements satisfy the predicate.

ArgumentException Thrown when the input lists differ in length.
Example

Check if the sum of pairs (from 2 different lists) have at least one even number

 let anEvenSum a b  = 0 = (a + b) % 2
 
 ([1..4], [2..5]) 
 ||> List.exists2 anEvenSum     // evaluates false
 
 ([1..4], [2;4;5;6])
 ||> List.exists2 anEvenSum   // evaluates true

List.filter predicate list

Full Usage: List.filter predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: 'T list A list containing only the elements that satisfy the predicate.

Returns a new collection containing only the elements of the collection for which the given predicate returns "true"

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: 'T list

A list containing only the elements that satisfy the predicate.

Example

 let input = [1, "Luke"; 2, "Kirk"; 3, "Kenobi"; 4, "Spock"]

 let isComingFromStarTrek (x,_) = isEven x
 
 input |> List.filter isComingFromStarTrek
Evaluates to [(2, "Kirk"); (4, "Spock")]

List.find predicate list

Full Usage: List.find predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: 'T The first element that satisfies the predicate.

Returns the first element for which the given function returns True. Raises KeyNotFoundException if no such element exists.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: 'T

The first element that satisfies the predicate.

KeyNotFoundException Thrown if the predicate evaluates to false for all the elements of the list.
Example

 let isEven x  = 0 = x % 2

 let isGreaterThan x y = y > x

 let input = [1, "Luke"; 2, "Kirk"; 3, "Spock"; 4, "Kenobi"]
 
 input |> List.find (fun (x,_) -> isEven x)              // evaluates (2, "Kirk")
 input |> List.find (fun (x,_) -> x |> isGreaterThan 6)  // raises an exception

List.findBack predicate list

Full Usage: List.findBack predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: 'T The last element that satisfies the predicate.

Returns the last element for which the given function returns True. Raises KeyNotFoundException if no such element exists.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: 'T

The last element that satisfies the predicate.

KeyNotFoundException Thrown if the predicate evaluates to false for all the elements of the list.
Example

 let isEven x  = 0 = x % 2

 let isGreaterThan x y = y > x

 let input = [1, "Luke"; 2, "Kirk"; 3, "Spock"; 4, "Kenobi"]
 
 input |> List.findBack (fun (x,_) -> isEven x)              // evaluates (4, "Kenobi")
 input |> List.findBack (fun (x,_) -> x |> isGreaterThan 6)  // raises an exception

List.findIndex predicate list

Full Usage: List.findIndex predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: int The index of the first element that satisfies the predicate.

Returns the index of the first element in the list that satisfies the given predicate. Raises KeyNotFoundException if no such element exists.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: int

The index of the first element that satisfies the predicate.

ArgumentException Thrown if the predicate evaluates to false for all the elements of the list.
Example

 let isEven x  = 0 = x % 2

 let isGreaterThan x y = y > x

 let input = [1, "Luke"; 2, "Kirk"; 3, "Spock"; 4, "Kenobi"]
 
 input |> List.findIndex (fun (x,_) -> isEven x)              // evaluates 1
 input |> List.findIndex (fun (x,_) -> x |> isGreaterThan 6)  // raises an exception

List.findIndexBack predicate list

Full Usage: List.findIndexBack predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: int The index of the last element that satisfies the predicate.

Returns the index of the last element in the list that satisfies the given predicate. Raises KeyNotFoundException if no such element exists.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: int

The index of the last element that satisfies the predicate.

ArgumentException Thrown if the predicate evaluates to false for all the elements of the list.
Example

 let isEven x  = 0 = x % 2

 let isGreaterThan x y = y > x

 let input = [1, "Luke"; 2, "Kirk"; 3, "Spock"; 4, "Kenobi"]
 
 input |> List.findIndexBack (fun (x,_) -> isEven x)              // evaluates 3
 input |> List.findIndexBack (fun (x,_) -> x |> isGreaterThan 6)  // raises an exception

List.fold folder state list

Full Usage: List.fold folder state list

Parameters:
    folder : 'State -> 'T -> 'State - The function to update the state given the input elements.
    state : 'State - The initial state.
    list : 'T list - The input list.

Returns: 'State The final state value.

Applies a function to each element of the collection, threading an accumulator argument through the computation. Take the second argument, and apply the function to it and the first element of the list. Then feed this result into the function along with the second element and so on. Return the final result. If the input function is f and the elements are i0...iN then computes f (... (f s i0) i1 ...) iN.

folder : 'State -> 'T -> 'State

The function to update the state given the input elements.

state : 'State

The initial state.

list : 'T list

The input list.

Returns: 'State

The final state value.

Example

Making the sum of squares for the first 5 natural numbers

 (0, [1..5]) ||> List.fold (fun s v -> s + v * v)  // evaluates 55

Example

Shopping for fruits hungry, you tend to take more of each as the hunger grows

 type Fruit = Apple | Pear | Orange

 type BagItem = { fruit: Fruit; quantity: int }

 let takeMore (previous: BagItem list) fruit = 
     let toTakeThisTime = 
         match previous with 
         | bagItem :: otherBagItems -> bagItem.quantity + 1 
         | [] -> 1 
     { fruit = fruit; quantity = toTakeThisTime } :: previous

 let inputs = [ Apple; Pear; Orange ]
 
 ([], inputs) ||> List.fold takeMore
Evaluates to
  [{ fruit = Orange; quantity = 3 }
   { fruit = Pear; quantity = 2 }
   { fruit = Apple; quantity = 1 }]

List.fold2 folder state list1 list2

Full Usage: List.fold2 folder state list1 list2

Parameters:
    folder : 'State -> 'T1 -> 'T2 -> 'State - The function to update the state given the input elements.
    state : 'State - The initial state.
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.

Returns: 'State The final state value.

Applies a function to corresponding elements of two collections, threading an accumulator argument through the computation. The collections must have identical sizes. If the input function is f and the elements are i0...iN and j0...jN then computes f (... (f s i0 j0)...) iN jN.

folder : 'State -> 'T1 -> 'T2 -> 'State

The function to update the state given the input elements.

state : 'State

The initial state.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

Returns: 'State

The final state value.

Example

Count the numer of times the coins match:
 type CoinToss = Head | Tails

 let inputs1 = [Tails; Head; Tails]
 let inputs2 = [Tails; Head; Head]

 (0, inputs1, inputs2) |||> List.fold2 (fun acc input1 input2 ->
     match (input1, input2) with
     | Head, Head -> acc + 1
     | Tails, Tails -> acc + 1
     | _ -> acc - 1)
Evaluates to 1. Note acc is a commonly used abbreviation for "accumulator".

List.foldBack folder list state

Full Usage: List.foldBack folder list state

Parameters:
    folder : 'T -> 'State -> 'State - The function to update the state given the input elements.
    list : 'T list - The input list.
    state : 'State - The initial state.

Returns: 'State The state object after the folding function is applied to each element of the list.

Applies a function to each element of the collection, starting from the end, threading an accumulator argument through the computation. If the input function is f and the elements are i0...iN then computes f i0 (...(f iN s)).

folder : 'T -> 'State -> 'State

The function to update the state given the input elements.

list : 'T list

The input list.

state : 'State

The initial state.

Returns: 'State

The state object after the folding function is applied to each element of the list.

Example

Making the sum of squares for the first 5 natural numbers

 ([1..5], 0) ||> List.foldBack (fun v acc -> acc + v * v)  // evaluates 55
Note acc is a commonly used abbreviation for "accumulator".

Example

Shopping for fruits hungry, you tend to take more of each as the hunger grows

 type Fruit = Apple | Pear | Orange

 type BagItem = { fruit: Fruit; quantity: int }

 let takeMore fruit (previous: BagItem list) =
     let toTakeThisTime = 
         match previous with 
         | bagItem :: otherBagItems -> bagItem.quantity + 1 
         | [] -> 1 
     { fruit = fruit; quantity = toTakeThisTime } :: previous

 let input = [ Apple; Pear; Orange ]
 
 (input, []) ||> List.foldBack takeMore
Evaluates to
  [{ fruit = Apple; quantity = 3 }
   { fruit = Pear; quantity = 2 }
   { fruit = Orange; quantity = 1 }]

List.foldBack2 folder list1 list2 state

Full Usage: List.foldBack2 folder list1 list2 state

Parameters:
    folder : 'T1 -> 'T2 -> 'State -> 'State - The function to update the state given the input elements.
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.
    state : 'State - The initial state.

Returns: 'State The final state value.

Applies a function to corresponding elements of two collections, threading an accumulator argument through the computation. The collections must have identical sizes. If the input function is f and the elements are i0...iN and j0...jN then computes f i0 j0 (...(f iN jN s)).

folder : 'T1 -> 'T2 -> 'State -> 'State

The function to update the state given the input elements.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

state : 'State

The initial state.

Returns: 'State

The final state value.

Example

Count the positives, negatives and accumulate some text from back to front:
 type Count =
   { Positive: int
     Negative: int
     Text: string }

 let inputs1 = [ -1; -2; -3 ]
 let inputs2 = [ 3; 2; 1 ]
 let initialState = {Positive = 0; Negative = 0; Text = ""}

 (inputs1, inputs2, initialState) |||> List.foldBack2 (fun a b acc  ->
     let text = acc.Text + "(" + string a + "," + string b + ") "
     if a + b >= 0 then
         { acc with
             Positive = acc.Positive + 1
             Text = text }
     else
         { acc with
             Negative = acc.Negative + 1
             Text = text }
 )
Evaluates to
 { Positive = 2
   Negative = 1
   Text = "(-3,1) (-2,2) (-1,3) " }
Note acc is a commonly used abbreviation for "accumulator".

List.forall predicate list

Full Usage: List.forall predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: bool True if all of the elements satisfy the predicate.

Tests if all elements of the collection satisfy the given predicate.

The predicate is applied to the elements of the input list. If any application returns false then the overall result is false and no further elements are tested. Otherwise, true is returned.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: bool

True if all of the elements satisfy the predicate.

Example

 let isEven a = a % 2 = 0

 [2; 42] |> List.forall isEven // evaluates to true

 [1; 2] |> List.forall isEven // evaluates to false

List.forall2 predicate list1 list2

Full Usage: List.forall2 predicate list1 list2

Parameters:
    predicate : 'T1 -> 'T2 -> bool - The function to test the input elements.
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.

Returns: bool True if all of the pairs of elements satisfy the predicate.

Tests if all corresponding elements of the collection satisfy the given predicate pairwise.

The predicate is applied to matching elements in the two collections up to the lesser of the two lengths of the collections. If any application returns false then the overall result is false and no further elements are tested. Otherwise, if one collection is longer than the other then the ArgumentException exception is raised. Otherwise, true is returned.

predicate : 'T1 -> 'T2 -> bool

The function to test the input elements.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

Returns: bool

True if all of the pairs of elements satisfy the predicate.

ArgumentException Thrown when the input lists differ in length.
Example

 let inputs1 = [1; 2; 3]
 let inputs2 = [1; 2; 3]

 (inputs1, inputs2) ||> List.forall2 (=)
Evaluates to true.

Example

 let items1 = [2017; 1; 1]
 let items2 = [2019; 19; 8]

 (items1, items2) ||> List.forall2 (=)
Evaluates to false.

Example

 let items1 = [1; 2; 3]
 let items2 = [1; 2]

 (items1, items2) ||> List.forall2 (=)
Throws ArgumentException.

List.groupBy projection list

Full Usage: List.groupBy projection list

Parameters:
    projection : 'T -> 'Key - A function that transforms an element of the list into a comparable key.
    list : 'T list - The input list.

Returns: ('Key * 'T list) list The result list.

Applies a key-generating function to each element of a list and yields a list of unique keys. Each unique key contains a list of all elements that match to this key.

projection : 'T -> 'Key

A function that transforms an element of the list into a comparable key.

list : 'T list

The input list.

Returns: ('Key * 'T list) list

The result list.

Example

 let inputs = [1; 2; 3; 4; 5]

 inputs |> List.groupBy (fun n -> n % 2)
Evaluates to [(1, [1; 3; 5]); (0, [2; 4])]

List.head list

Full Usage: List.head list

Parameters:
    list : 'T list - The input list.

Returns: 'T The first element of the list.

Returns the first element of the list.

list : 'T list

The input list.

Returns: 'T

The first element of the list.

ArgumentException Thrown when the list is empty.
Example

 let inputs = ["banana"; "pear"]

 inputs |> List.head
Evaluates to banana

Example

 [] |> List.head
Throws ArgumentException

List.indexed list

Full Usage: List.indexed list

Parameters:
    list : 'T list - The input list.

Returns: (int * 'T) list The list of indexed elements.

Returns a new list whose elements are the corresponding elements of the input list paired with the index (from 0) of each element.

list : 'T list

The input list.

Returns: (int * 'T) list

The list of indexed elements.

Example

 let inputs = ["a"; "b"; "c"]

 inputs |> List.indexed
Evaluates to [(0, "a"); (1, "b"); (2, "c")]

List.init length initializer

Full Usage: List.init length initializer

Parameters:
    length : int - The length of the list to generate.
    initializer : int -> 'T - The function to generate an element from an index.

Returns: 'T list The list of generated elements.

Creates a list by calling the given generator on each index.

length : int

The length of the list to generate.

initializer : int -> 'T

The function to generate an element from an index.

Returns: 'T list

The list of generated elements.

ArgumentException Thrown when the input length is negative.
Example

 List.init 4 (fun v -> v + 5)
Evaluates to [5; 6; 7; 8]

Example

 List.init -5 (fun v -> v + 5)
Throws ArgumentException

List.insertAt index value source

Full Usage: List.insertAt index value source

Parameters:
    index : int - The index where the item should be inserted.
    value : 'T - The value to insert.
    source : 'T list - The input list.

Returns: 'T list The result list.

Return a new list with a new item inserted before the given index.

index : int

The index where the item should be inserted.

value : 'T

The value to insert.

source : 'T list

The input list.

Returns: 'T list

The result list.

ArgumentException Thrown when index is below 0 or greater than source.Length.
Example

 let inputs = [ 0; 1; 2 ]

 inputs |> List.insertAt 1 9
Evaluates to [ 0; 9; 1; 2 ].

List.insertManyAt index values source

Full Usage: List.insertManyAt index values source

Parameters:
    index : int - The index where the items should be inserted.
    values : seq<'T> - The values to insert.
    source : 'T list - The input list.

Returns: 'T list The result list.

Return a new list with new items inserted before the given index.

index : int

The index where the items should be inserted.

values : seq<'T>

The values to insert.

source : 'T list

The input list.

Returns: 'T list

The result list.

ArgumentException Thrown when index is below 0 or greater than source.Length.
Example

 let inputs = [ 0; 1; 2 ]

 inputs |> List.insertManyAt 1 [ 8; 9 ]
 Evaluates to [ 0; 8; 9; 1; 2 ].

List.isEmpty list

Full Usage: List.isEmpty list

Parameters:
    list : 'T list - The input list.

Returns: bool True if the list is empty.

Returns true if the list contains no elements, false otherwise.

list : 'T list

The input list.

Returns: bool

True if the list is empty.

Example

 [ ] |> List.isEmpty
Evaluates to true

Example

 [ "pear"; "banana" ] |> List.isEmpty
Evaluates to false

List.item index list

Full Usage: List.item index list

Parameters:
    index : int - The index to retrieve.
    list : 'T list - The input list.

Returns: 'T The value at the given index.

Indexes into the list. The first element has index 0.

index : int

The index to retrieve.

list : 'T list

The input list.

Returns: 'T

The value at the given index.

ArgumentException Thrown when the index is negative or the input list does not contain enough elements.
Example

 let inputs = [ "a"; "b"; "c" ]

 inputs |> List.item 1
Evaluates to "b"

Example

 let inputs = [ "a"; "b"; "c" ]

 inputs |> List.item 4
Throws ArgumentException

List.iter action list

Full Usage: List.iter action list

Parameters:
    action : 'T -> unit - The function to apply to elements from the input list.
    list : 'T list - The input list.

Modifiers: inline
Type parameters: 'T

Applies the given function to each element of the collection.

action : 'T -> unit

The function to apply to elements from the input list.

list : 'T list

The input list.

Example

 let inputs = [ "a"; "b"; "c" ]

 inputs |> List.iter (printfn "%s")
Evaluates to unit and prints
 a
 b
 c
in the console.

List.iter2 action list1 list2

Full Usage: List.iter2 action list1 list2

Parameters:
    action : 'T1 -> 'T2 -> unit - The function to apply to pairs of elements from the input lists.
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.

Applies the given function to two collections simultaneously. The collections must have identical size.

action : 'T1 -> 'T2 -> unit

The function to apply to pairs of elements from the input lists.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

Example

 let inputs1 = [ "a"; "b"; "c" ]
 let inputs2 = [ 1; 2; 3 ]

 (inputs1, inputs2) ||> List.iter2 (printfn "%s: %i")
Evaluates to unit and prints
 a: 1
 b: 2
 c: 3
in the console.

List.iteri action list

Full Usage: List.iteri action list

Parameters:
    action : int -> 'T -> unit - The function to apply to the elements of the list along with their index.
    list : 'T list - The input list.

Modifiers: inline
Type parameters: 'T

Applies the given function to each element of the collection. The integer passed to the function indicates the index of element.

action : int -> 'T -> unit

The function to apply to the elements of the list along with their index.

list : 'T list

The input list.

Example

 let inputs = [ "a"; "b"; "c" ]

 inputs |> List.iteri (fun i v -> printfn "{i}: {v}")
Evaluates to unit and prints
 0: a
 1: b
 2: c
in the console.

List.iteri2 action list1 list2

Full Usage: List.iteri2 action list1 list2

Parameters:
    action : int -> 'T1 -> 'T2 -> unit - The function to apply to a pair of elements from the input lists along with their index.
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.

Applies the given function to two collections simultaneously. The collections must have identical size. The integer passed to the function indicates the index of element.

action : int -> 'T1 -> 'T2 -> unit

The function to apply to a pair of elements from the input lists along with their index.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

Example

 let inputs1 = [ "a"; "b"; "c" ]
 let inputs2 = [ "banana"; "pear"; "apple" ]

 (inputs1, inputs2) ||> List.iteri2 (fun i s1 s2 -> printfn "Index {i}: {s1} - {s2}")
Evaluates to unit and prints
 Index 0: a - banana
 Index 1: b - pear
 Index 2: c - apple
in the console.

List.last list

Full Usage: List.last list

Parameters:
    list : 'T list - The input list.

Returns: 'T The last element of the list.

Returns the last element of the list.

list : 'T list

The input list.

Returns: 'T

The last element of the list.

ArgumentException Thrown when the input does not have any elements.
Example

 [ "pear"; "banana" ] |> List.last
Evaluates to banana

Example

 [ ] |> List.last
Throws ArgumentException

List.length list

Full Usage: List.length list

Parameters:
    list : 'T list - The input list.

Returns: int The length of the list.

Returns the length of the list.

The notation array.Length is preferred.

list : 'T list

The input list.

Returns: int

The length of the list.

Example

 let inputs = [ "a"; "b"; "c" ]

 inputs |> List.length
Evaluates to 3

List.map mapping list

Full Usage: List.map mapping list

Parameters:
    mapping : 'T -> 'U - The function to transform elements from the input list.
    list : 'T list - The input list.

Returns: 'U list The list of transformed elements.

Builds a new collection whose elements are the results of applying the given function to each of the elements of the collection.

mapping : 'T -> 'U

The function to transform elements from the input list.

list : 'T list

The input list.

Returns: 'U list

The list of transformed elements.

Example

 let inputs = [ "a"; "bbb"; "cc" ]

 inputs |> List.map (fun x -> x.Length)
Evaluates to [ 1; 3; 2 ]

List.map2 mapping list1 list2

Full Usage: List.map2 mapping list1 list2

Parameters:
    mapping : 'T1 -> 'T2 -> 'U - The function to transform pairs of elements from the input lists.
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.

Returns: 'U list The list of transformed elements.

Builds a new collection whose elements are the results of applying the given function to the corresponding elements of the two collections pairwise.

mapping : 'T1 -> 'T2 -> 'U

The function to transform pairs of elements from the input lists.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

Returns: 'U list

The list of transformed elements.

Example

 let inputs1 = ["a"; "bad"; "good"]
 let inputs2 = [0; 2; 1]

 (inputs1, inputs2) ||> List.map2 (fun x y -> x.[y])
Evaluates to seq ['a'; 'd'; 'o']

List.map3 mapping list1 list2 list3

Full Usage: List.map3 mapping list1 list2 list3

Parameters:
    mapping : 'T1 -> 'T2 -> 'T3 -> 'U - The function to transform triples of elements from the input lists.
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.
    list3 : 'T3 list - The third input list.

Returns: 'U list The list of transformed elements.

Builds a new collection whose elements are the results of applying the given function to the corresponding elements of the three collections simultaneously.

mapping : 'T1 -> 'T2 -> 'T3 -> 'U

The function to transform triples of elements from the input lists.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

list3 : 'T3 list

The third input list.

Returns: 'U list

The list of transformed elements.

Example

 let inputs1 = [ "a"; "t"; "ti" ]
 let inputs2 = [ "l"; "h"; "m" ]
 let inputs3 = [ "l"; "e"; "e" ]

 (inputs1, inputs2, inputs3) |||> List.map3 (fun x y z -> x + y + z)
Evaluates to [ "all"; "the"; "time" ]

List.mapFold mapping state list

Full Usage: List.mapFold mapping state list

Parameters:
    mapping : 'State -> 'T -> 'Result * 'State - The function to transform elements from the input list and accumulate the final value.
    state : 'State - The initial state.
    list : 'T list - The input list.

Returns: 'Result list * 'State The list of transformed elements, and the final accumulated value.

Combines map and fold. Builds a new list whose elements are the results of applying the given function to each of the elements of the input list. The function is also used to accumulate a final value.

mapping : 'State -> 'T -> 'Result * 'State

The function to transform elements from the input list and accumulate the final value.

state : 'State

The initial state.

list : 'T list

The input list.

Returns: 'Result list * 'State

The list of transformed elements, and the final accumulated value.

Example

Accumulate the charges, and double them as well
 type Charge =
     | In of int
     | Out of int

 let inputs = [ In 1; Out 2; In 3 ]

 let newCharges, balance =
     (0, inputs) ||> List.mapFold (fun acc charge ->
         match charge with
         | In i -> In (i*2), acc + i
         | Out o -> Out (o*2), acc - o)
Evaluates newCharges to [In 2; Out 4; In 6] and balance to 2. Note acc is a commonly used abbreviation for "accumulator".

List.mapFoldBack mapping list state

Full Usage: List.mapFoldBack mapping list state

Parameters:
    mapping : 'T -> 'State -> 'Result * 'State - The function to transform elements from the input list and accumulate the final value.
    list : 'T list - The input list.
    state : 'State - The initial state.

Returns: 'Result list * 'State The list of transformed elements, and the final accumulated value.

Combines map and foldBack. Builds a new list whose elements are the results of applying the given function to each of the elements of the input list. The function is also used to accumulate a final value.

mapping : 'T -> 'State -> 'Result * 'State

The function to transform elements from the input list and accumulate the final value.

list : 'T list

The input list.

state : 'State

The initial state.

Returns: 'Result list * 'State

The list of transformed elements, and the final accumulated value.

Example

Accumulate the charges from back to front, and double them as well

 type Charge =
     | In of int
     | Out of int

 let charges = [ In 1; Out 2; In 3 ]

 let newCharges, balance =
     (charges, 0) ||> List.mapFoldBack (fun charge acc ->
         match charge with
         | In i -> In (i*2), acc + i
         | Out o -> Out (o*2), acc - o)
Evaluates newCharges to [In 2; Out 4; In 6] and balance to 2. Note acc is a commonly used abbreviation for "accumulator".

List.mapi mapping list

Full Usage: List.mapi mapping list

Parameters:
    mapping : int -> 'T -> 'U - The function to transform elements and their indices.
    list : 'T list - The input list.

Returns: 'U list The list of transformed elements.

Builds a new collection whose elements are the results of applying the given function to each of the elements of the collection. The integer index passed to the function indicates the index (from 0) of element being transformed.

mapping : int -> 'T -> 'U

The function to transform elements and their indices.

list : 'T list

The input list.

Returns: 'U list

The list of transformed elements.

Example

 let inputs = [ 10; 10; 10 ]

 inputs |> List.mapi (fun i x -> i + x)
Evaluates to [ 10; 11; 12 ]

List.mapi2 mapping list1 list2

Full Usage: List.mapi2 mapping list1 list2

Parameters:
    mapping : int -> 'T1 -> 'T2 -> 'U - The function to transform pairs of elements from the two lists and their index.
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.

Returns: 'U list The list of transformed elements.

Like mapi, but mapping corresponding elements from two lists of equal length.

mapping : int -> 'T1 -> 'T2 -> 'U

The function to transform pairs of elements from the two lists and their index.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

Returns: 'U list

The list of transformed elements.

Example

 let inputs1 = ["a"; "bad"; "good"]
 let inputs2 = [0; 2; 1]

 (inputs1, inputs2) ||> List.mapi2 (fun i x y -> i, x[y])
Evaluates to [(0, 'a'); (1, 'd'); (2, 'o')]

List.max list

Full Usage: List.max list

Parameters:
    list : 'T list - The input list.

Returns: 'T The maximum element.
Modifiers: inline
Type parameters: 'T

Return the greatest of all elements of the list, compared via Operators.max.

Raises ArgumentException if list is empty

list : 'T list

The input list.

Returns: 'T

The maximum element.

ArgumentException Thrown when the list is empty.
Example

 let inputs = [ 10; 12; 11 ]

 inputs |> List.max
Evaluates to 12

Example

 let inputs = [ ]

 inputs |> List.max
Throws System.ArgumentException.

List.maxBy projection list

Full Usage: List.maxBy projection list

Parameters:
    projection : 'T -> 'U - The function to transform the list elements into the type to be compared.
    list : 'T list - The input list.

Returns: 'T The maximum element.
Modifiers: inline
Type parameters: 'T, 'U

Returns the greatest of all elements of the list, compared via Operators.max on the function result.

Raises ArgumentException if list is empty.

projection : 'T -> 'U

The function to transform the list elements into the type to be compared.

list : 'T list

The input list.

Returns: 'T

The maximum element.

ArgumentException Thrown when the list is empty.
Example

 let inputs = ["aaa"; "b"; "cccc"]

 inputs |> List.maxBy (fun s -> s.Length)
Evaluates to "cccc"

Example

 let inputs = []

 inputs |> List.maxBy (fun (s: string) -> s.Length)
Throws System.ArgumentException.

List.min list

Full Usage: List.min list

Parameters:
    list : 'T list - The input list.

Returns: 'T The minimum value.
Modifiers: inline
Type parameters: 'T

Returns the lowest of all elements of the list, compared via Operators.min.

Raises ArgumentException if list is empty

list : 'T list

The input list.

Returns: 'T

The minimum value.

ArgumentException Thrown when the list is empty.
Example

 let inputs = [10; 12; 11]

 inputs |> List.min
Evaluates to 10

Example

 let inputs = []

 inputs |> List.min
Throws System.ArgumentException.

List.minBy projection list

Full Usage: List.minBy projection list

Parameters:
    projection : 'T -> 'U - The function to transform list elements into the type to be compared.
    list : 'T list - The input list.

Returns: 'T The minimum value.
Modifiers: inline
Type parameters: 'T, 'U

Returns the lowest of all elements of the list, compared via Operators.min on the function result

Raises ArgumentException if list is empty.

projection : 'T -> 'U

The function to transform list elements into the type to be compared.

list : 'T list

The input list.

Returns: 'T

The minimum value.

ArgumentException Thrown when the list is empty.
Example

 let inputs = ["aaa"; "b"; "cccc"]

 inputs |> List.minBy (fun s -> s.Length)
Evaluates to "b"

Example

 let inputs = []

 inputs |> List.minBy (fun (s: string) -> s.Length)
Throws System.ArgumentException.

List.ofArray array

Full Usage: List.ofArray array

Parameters:
    array : 'T[] - The input array.

Returns: 'T list The list of elements from the array.

Builds a list from the given array.

array : 'T[]

The input array.

Returns: 'T list

The list of elements from the array.

Example

 let inputs = [| 1; 2; 5 |]

 inputs |> List.ofArray
Evaluates to [ 1; 2; 5 ].

List.ofSeq source

Full Usage: List.ofSeq source

Parameters:
    source : seq<'T> - The input sequence.

Returns: 'T list The list of elements from the sequence.

Builds a new list from the given enumerable object.

source : seq<'T>

The input sequence.

Returns: 'T list

The list of elements from the sequence.

Example

 let inputs = seq { 1; 2; 5 }

 inputs |> List.ofSeq
Evaluates to [ 1; 2; 5 ].

List.pairwise list

Full Usage: List.pairwise list

Parameters:
    list : 'T list - The input list.

Returns: ('T * 'T) list The result list.

Returns a list of each element in the input list and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element.

list : 'T list

The input list.

Returns: ('T * 'T) list

The result list.

Example

 let inputs = [1; 2; 3; 4]

 inputs |> List.pairwise
Evaluates to [(1, 2); (2, 3); (3, 4)].

List.partition predicate list

Full Usage: List.partition predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: 'T list * 'T list A list containing the elements for which the predicate evaluated to true and a list containing the elements for which the predicate evaluated to false.

Splits the collection into two collections, containing the elements for which the given predicate returns True and False respectively. Element order is preserved in both of the created lists.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: 'T list * 'T list

A list containing the elements for which the predicate evaluated to true and a list containing the elements for which the predicate evaluated to false.

Example

 let inputs = [1; 2; 3; 4]

 let evens, odds = inputs |> List.partition (fun x -> x % 2 = 0)
Evaluates evens to [2; 4] and odds to [1; 3].

List.permute indexMap list

Full Usage: List.permute indexMap list

Parameters:
    indexMap : int -> int - The function to map input indices to output indices.
    list : 'T list - The input list.

Returns: 'T list The permuted list.

Returns a list with all elements permuted according to the specified permutation.

indexMap : int -> int

The function to map input indices to output indices.

list : 'T list

The input list.

Returns: 'T list

The permuted list.

ArgumentException Thrown when indexMap does not produce a valid permutation.
Example

 let inputs = [1; 2; 3; 4]

 inputs |> List.permute (fun x -> (x + 1) % 4)
Evaluates to [4; 1; 2; 3].

List.pick chooser list

Full Usage: List.pick chooser list

Parameters:
    chooser : 'T -> 'U option - The function to generate options from the elements.
    list : 'T list - The input list.

Returns: 'U The first resulting value.

Applies the given function to successive elements, returning the first result where function returns Some(x) for some x. If no such element exists then raise KeyNotFoundException

chooser : 'T -> 'U option

The function to generate options from the elements.

list : 'T list

The input list.

Returns: 'U

The first resulting value.

KeyNotFoundException Thrown when the list is empty.
Example

 let input = [1; 2; 3]

 input |> List.pick (fun n -> if n % 2 = 0 then Some (string n) else None)
Evaluates to "2".

Example

 let input = [1; 2; 3]

 input |> List.pick (fun n -> if n > 3 = 0 then Some (string n) else None)
Throws KeyNotFoundException.

List.reduce reduction list

Full Usage: List.reduce reduction list

Parameters:
    reduction : 'T -> 'T -> 'T - The function to reduce two list elements to a single element.
    list : 'T list - The input list.

Returns: 'T The final reduced value.

Apply a function to each element of the collection, threading an accumulator argument through the computation. Apply the function to the first two elements of the list. Then feed this result into the function along with the third element and so on. Return the final result. If the input function is f and the elements are i0...iN then computes f (... (f i0 i1) i2 ...) iN.

Raises ArgumentException if list is empty

reduction : 'T -> 'T -> 'T

The function to reduce two list elements to a single element.

list : 'T list

The input list.

Returns: 'T

The final reduced value.

ArgumentException Thrown when the list is empty.
Example

 let inputs = [1; 3; 4; 2]

 inputs |> List.reduce (fun a b -> a * 10 + b)
Evaluates to 1342, by computing ((1 * 10 + 3) * 10 + 4) * 10 + 2

List.reduceBack reduction list

Full Usage: List.reduceBack reduction list

Parameters:
    reduction : 'T -> 'T -> 'T - A function that takes in the next-to-last element of the list and the current accumulated result to produce the next accumulated result.
    list : 'T list - The input list.

Returns: 'T The final result of the reductions.

Applies a function to each element of the collection, starting from the end, threading an accumulator argument through the computation. If the input function is f and the elements are i0...iN then computes f i0 (...(f iN-1 iN)).

reduction : 'T -> 'T -> 'T

A function that takes in the next-to-last element of the list and the current accumulated result to produce the next accumulated result.

list : 'T list

The input list.

Returns: 'T

The final result of the reductions.

ArgumentException Thrown when the list is empty.
Example

 let inputs = [1; 3; 4; 2]

 inputs |> List.reduceBack (fun a b -> a + b * 10)
Evaluates to 2431, by computing 1 + (3 + (4 + 2 * 10) * 10) * 10

List.removeAt index source

Full Usage: List.removeAt index source

Parameters:
    index : int - The index of the item to be removed.
    source : 'T list - The input list.

Returns: 'T list The result list.

Return a new list with the item at a given index removed.

index : int

The index of the item to be removed.

source : 'T list

The input list.

Returns: 'T list

The result list.

ArgumentException Thrown when index is outside 0..source.Length - 1
Example

 let inputs = [ 0; 1; 2 ]

 inputs |> List.removeAt 1
let inputs = [ 0; 2 ]

List.removeManyAt index count source

Full Usage: List.removeManyAt index count source

Parameters:
    index : int - The index of the item to be removed.
    count : int - The number of items to remove.
    source : 'T list - The input list.

Returns: 'T list The result list.

Return a new list with the number of items starting at a given index removed.

index : int

The index of the item to be removed.

count : int

The number of items to remove.

source : 'T list

The input list.

Returns: 'T list

The result list.

ArgumentException Thrown when index is outside 0..source.Length - count
Example

 let inputs = [ 0; 1; 2; 3 ]

 inputs |> List.removeManyAt 1 2
Evaluates to [ 0; 3 ].

List.replicate count initial

Full Usage: List.replicate count initial

Parameters:
    count : int - The number of elements to replicate.
    initial : 'T - The value to replicate

Returns: 'T list The generated list.

Creates a list by replicating the given initial value.

count : int

The number of elements to replicate.

initial : 'T

The value to replicate

Returns: 'T list

The generated list.

Example

 List.replicate 3 "a"
Evaluates to [ "a"; "a"; "a" ].

List.rev list

Full Usage: List.rev list

Parameters:
    list : 'T list - The input list.

Returns: 'T list The reversed list.

Returns a new list with the elements in reverse order.

list : 'T list

The input list.

Returns: 'T list

The reversed list.

Example

 let inputs = [ 0; 1; 2 ]

 inputs |> List.rev
Evaluates to [ 2; 1; 0 ].

List.scan folder state list

Full Usage: List.scan folder state list

Parameters:
    folder : 'State -> 'T -> 'State - The function to update the state given the input elements.
    state : 'State - The initial state.
    list : 'T list - The input list.

Returns: 'State list The list of states.

Applies a function to each element of the collection, threading an accumulator argument through the computation. Take the second argument, and apply the function to it and the first element of the list. Then feed this result into the function along with the second element and so on. Returns the list of intermediate results and the final result.

folder : 'State -> 'T -> 'State

The function to update the state given the input elements.

state : 'State

The initial state.

list : 'T list

The input list.

Returns: 'State list

The list of states.

Example

Apply a list charges and collect the running balances as each is applied:

 type Charge =
     | In of int
     | Out of int

 let inputs = [In 1; Out 2; In 3]

 (0, inputs) ||> List.scan (fun acc charge ->
     match charge with
     | In i -> acc + i
     | Out o -> acc - o)
Evaluates to [0; 1; -1; 2]. Note 0 is the intial state, 1 the next state, -1 the next state, and 2 the final state. Note acc is a commonly used abbreviation for "accumulator".

List.scanBack folder list state

Full Usage: List.scanBack folder list state

Parameters:
    folder : 'T -> 'State -> 'State - The function to update the state given the input elements.
    list : 'T list - The input list.
    state : 'State - The initial state.

Returns: 'State list The list of states.

Like foldBack, but returns both the intermediary and final results

folder : 'T -> 'State -> 'State

The function to update the state given the input elements.

list : 'T list

The input list.

state : 'State

The initial state.

Returns: 'State list

The list of states.

Example

Apply a list charges from back to front, and collect the running balances as each is applied:

 type Charge =
     | In of int
     | Out of int

 let inputs = [ In 1; Out 2; In 3 ]

 (inputs, 0) ||> List.scanBack (fun charge acc ->
     match charge with
     | In i -> acc + i
     | Out o -> acc - o)
Evaluates to [2; 1; 3; 0] by processing each input from back to front. Note 0 is the intial state, 3 the next state, 1 the next state, and 2 the final state, and the states are produced from back to front. Note acc is a commonly used abbreviation for "accumulator".

List.singleton value

Full Usage: List.singleton value

Parameters:
    value : 'T - The input item.

Returns: 'T list The result list of one item.
Modifiers: inline
Type parameters: 'T

Returns a list that contains one item only.

value : 'T

The input item.

Returns: 'T list

The result list of one item.

Example

 List.singleton 7
Evaluates to [ 7 ].

List.skip count list

Full Usage: List.skip count list

Parameters:
    count : int - The number of elements to skip. If the number is 0 or negative the input list is returned.
    list : 'T list - The input list.

Returns: 'T list The list after removing the first N elements.

Returns the list after removing the first N elements.

count : int

The number of elements to skip. If the number is 0 or negative the input list is returned.

list : 'T list

The input list.

Returns: 'T list

The list after removing the first N elements.

ArgumentException Thrown when count exceeds the number of elements in the list.
Example

 let inputs = ["a"; "b"; "c"; "d"]

 inputs |> List.skip 2
Evaluates to ["c"; "d"]

Example

 let inputs = ["a"; "b"; "c"; "d"]

 inputs |> List.skip 5
Throws ArgumentException.

Example

 let inputs = ["a"; "b"; "c"; "d"]

 inputs |> List.skip -1
Evaluates to ["a"; "b"; "c"; "d"].

List.skipWhile predicate list

Full Usage: List.skipWhile predicate list

Parameters:
    predicate : 'T -> bool - A function that evaluates an element of the list to a boolean value.
    list : 'T list - The input list.

Returns: 'T list The result list.

Bypasses elements in a list while the given predicate returns True, and then returns the remaining elements of the list.

predicate : 'T -> bool

A function that evaluates an element of the list to a boolean value.

list : 'T list

The input list.

Returns: 'T list

The result list.

Example

 let inputs = ["a"; "bbb"; "cc"; "d"]

 inputs |> List.skipWhile (fun x -> x.Length < 3)
Evaluates to ["bbb"; "cc"; "d"]

List.sort list

Full Usage: List.sort list

Parameters:
    list : 'T list - The input list.

Returns: 'T list The sorted list.

Sorts the given list using Operators.compare.

This is a stable sort, i.e. the original order of equal elements is preserved.

list : 'T list

The input list.

Returns: 'T list

The sorted list.

Example

 let input = [8; 4; 3; 1; 6; 1]

 List.sort input
Evaluates to [1; 1 3; 4; 6; 8].

List.sortBy projection list

Full Usage: List.sortBy projection list

Parameters:
    projection : 'T -> 'Key - The function to transform the list elements into the type to be compared.
    list : 'T list - The input list.

Returns: 'T list The sorted list.

Sorts the given list using keys given by the given projection. Keys are compared using Operators.compare.

This is a stable sort, i.e. the original order of equal elements is preserved.

projection : 'T -> 'Key

The function to transform the list elements into the type to be compared.

list : 'T list

The input list.

Returns: 'T list

The sorted list.

Example

 let input = [ "a"; "bbb"; "cccc"; "dd" ]

 input |> List.sortBy (fun s -> s.Length)
Evaluates to ["a"; "dd"; "bbb"; "cccc"].

List.sortByDescending projection list

Full Usage: List.sortByDescending projection list

Parameters:
    projection : 'T -> 'Key - The function to transform the list elements into the type to be compared.
    list : 'T list - The input list.

Returns: 'T list The sorted list.
Modifiers: inline
Type parameters: 'T, 'Key

Sorts the given list in descending order using keys given by the given projection. Keys are compared using Operators.compare.

This is a stable sort, i.e. the original order of equal elements is preserved.

projection : 'T -> 'Key

The function to transform the list elements into the type to be compared.

list : 'T list

The input list.

Returns: 'T list

The sorted list.

Example

 let input = ["a"; "bbb"; "cccc"; "dd"]

 input |> List.sortByDescending (fun s -> s.Length)
Evaluates to ["cccc"; "bbb"; "dd"; "a"].

List.sortDescending list

Full Usage: List.sortDescending list

Parameters:
    list : 'T list - The input list.

Returns: 'T list The sorted list.
Modifiers: inline
Type parameters: 'T

Sorts the given list in descending order using Operators.compare.

This is a stable sort, i.e. the original order of equal elements is preserved.

list : 'T list

The input list.

Returns: 'T list

The sorted list.

Example

 let input = [8; 4; 3; 1; 6; 1]

 input |> List.sortDescending
Evaluates to [8; 6; 4; 3; 1; 1].

List.sortWith comparer list

Full Usage: List.sortWith comparer list

Parameters:
    comparer : 'T -> 'T -> int - The function to compare the list elements.
    list : 'T list - The input list.

Returns: 'T list The sorted list.

Sorts the given list using the given comparison function.

This is a stable sort, i.e. the original order of equal elements is preserved.

comparer : 'T -> 'T -> int

The function to compare the list elements.

list : 'T list

The input list.

Returns: 'T list

The sorted list.

Example

Sort a list of pairs using a comparison function that compares string lengths then index numbers:

 let compareEntries (n1: int, s1: string) (n2: int, s2: string) =
     let c = compare s1.Length s2.Length
     if c <> 0 then c else
     compare n1 n2

 let input = [ (0,"aa"); (1,"bbb"); (2,"cc"); (3,"dd") ]

 input |> List.sortWith compareEntries
Evaluates to [(0, "aa"); (2, "cc"); (3, "dd"); (1, "bbb")].

List.splitAt index list

Full Usage: List.splitAt index list

Parameters:
    index : int - The index at which the list is split.
    list : 'T list - The input list.

Returns: 'T list * 'T list The two split lists.

Splits a list into two lists, at the given index.

index : int

The index at which the list is split.

list : 'T list

The input list.

Returns: 'T list * 'T list

The two split lists.

InvalidOperationException Thrown when split index exceeds the number of elements in the list.
Example

 let input = [8; 4; 3; 1; 6; 1]

 let front, back = input |> List.splitAt 3
Evaluates front to [8; 4; 3] and back to [1; 6; 1].

List.splitInto count list

Full Usage: List.splitInto count list

Parameters:
    count : int - The maximum number of chunks.
    list : 'T list - The input list.

Returns: 'T list list The list split into chunks.

Splits the input list into at most count chunks.

count : int

The maximum number of chunks.

list : 'T list

The input list.

Returns: 'T list list

The list split into chunks.

ArgumentException Thrown when count is not positive.
Example

 [1..10] |> List.splitInto 2
Evaluates to [[1; 2; 3; 4; 5]; [6; 7; 8; 9; 10]].

Example

 [1..10] |> List.splitInto 4
Evaluates to [[1; 2; 3]; [4; 5; 6]; [7; 8]; [9; 10]].

List.sum list

Full Usage: List.sum list

Parameters:
    list : ^T list - The input list.

Returns: ^T The resulting sum.
Modifiers: inline
Type parameters: ^T

Returns the sum of the elements in the list.

list : ^T list

The input list.

Returns: ^T

The resulting sum.

Example

 let input = [ 1; 5; 3; 2 ]

 input |> List.sum
Evaluates to 11.

List.sumBy projection list

Full Usage: List.sumBy projection list

Parameters:
    projection : 'T -> ^U - The function to transform the list elements into the type to be summed.
    list : 'T list - The input list.

Returns: ^U The resulting sum.
Modifiers: inline
Type parameters: 'T, ^U

Returns the sum of the results generated by applying the function to each element of the list.

projection : 'T -> ^U

The function to transform the list elements into the type to be summed.

list : 'T list

The input list.

Returns: ^U

The resulting sum.

Example

 let input = [ "aa"; "bbb"; "cc" ]

 input |> List.sumBy (fun s -> s.Length)
Evaluates to 7.

List.tail list

Full Usage: List.tail list

Parameters:
    list : 'T list - The input list.

Returns: 'T list The list after removing the first element.

Returns the list after removing the first element.

list : 'T list

The input list.

Returns: 'T list

The list after removing the first element.

ArgumentException Thrown when the list is empty.
Example

 let inputs = ["a"; "bb"; "ccc"]

 inputs |> List.tail
Evaluates to ["bb"; "ccc"]

List.take count list

Full Usage: List.take count list

Parameters:
    count : int - The number of items to take.
    list : 'T list - The input list.

Returns: 'T list The result list.

Returns the first N elements of the list.

Throws InvalidOperationException if the count exceeds the number of elements in the list. List.truncate returns as many items as the list contains instead of throwing an exception.

count : int

The number of items to take.

list : 'T list

The input list.

Returns: 'T list

The result list.

ArgumentException Thrown when the input list is empty.
InvalidOperationException Thrown when count exceeds the number of elements in the list.
Example

 let inputs = ["a"; "b"; "c"; "d"]

 inputs |> List.take 2
Evaluates to ["a"; "b"]

Example

 let inputs = ["a"; "b"; "c"; "d"]

 inputs |> List.take 6
Throws InvalidOperationException.

Example

 let inputs = ["a"; "b"; "c"; "d"]

 inputs |> List.take 0
Evaluates to the empty list.

List.takeWhile predicate list

Full Usage: List.takeWhile predicate list

Parameters:
    predicate : 'T -> bool - A function that evaluates to false when no more items should be returned.
    list : 'T list - The input list.

Returns: 'T list The result list.

Returns a list that contains all elements of the original list while the given predicate returns True, and then returns no further elements.

predicate : 'T -> bool

A function that evaluates to false when no more items should be returned.

list : 'T list

The input list.

Returns: 'T list

The result list.

Example

 let inputs = ["a"; "bb"; "ccc"; "d"]

 inputs |> List.takeWhile (fun x -> x.Length < 3)
Evaluates to ["a"; "bb"]

List.toArray list

Full Usage: List.toArray list

Parameters:
    list : 'T list - The input list.

Returns: 'T[] The array containing the elements of the list.

Builds an array from the given list.

list : 'T list

The input list.

Returns: 'T[]

The array containing the elements of the list.

Example

 let inputs = [ 1; 2; 5 ]

 inputs |> List.toArray
Evaluates to [| 1; 2; 5 |].

List.toSeq list

Full Usage: List.toSeq list

Parameters:
    list : 'T list - The input list.

Returns: seq<'T> The sequence of elements in the list.

Views the given list as a sequence.

list : 'T list

The input list.

Returns: seq<'T>

The sequence of elements in the list.

Example

 let inputs = [ 1; 2; 5 ]

 inputs |> List.toSeq
Evaluates to seq { 1; 2; 5 }.

List.transpose lists

Full Usage: List.transpose lists

Parameters:
    lists : seq<'T list> - The input sequence of list.

Returns: 'T list list The transposed list.

Returns the transpose of the given sequence of lists.

lists : seq<'T list>

The input sequence of list.

Returns: 'T list list

The transposed list.

ArgumentNullException Thrown when the input sequence is null.
ArgumentException Thrown when the input lists differ in length.
Example

 let inputs =
     [ [ 10; 20; 30 ]
       [ 11; 21; 31 ] ]

 inputs |> List.transpose
Evaluates to [ [10; 11]; [20; 21]; [30; 31] ].

List.truncate count list

Full Usage: List.truncate count list

Parameters:
    count : int - The maximum number of items to return.
    list : 'T list - The input list.

Returns: 'T list The result list.

Returns at most N elements in a new list.

count : int

The maximum number of items to return.

list : 'T list

The input list.

Returns: 'T list

The result list.

Example

 let inputs = ["a"; "b"; "c"; "d"]

 inputs |> List.truncate 2
Evaluates to ["a"; "b"]

Example

 let inputs = ["a"; "b"; "c"; "d"]

 inputs |> List.truncate 6
Evaluates to ["a"; "b"; "c"; "d"]

Example

 let inputs = ["a"; "b"; "c"; "d"]

 inputs |> List.truncate 0
Evaluates to the empty list.

List.tryExactlyOne list

Full Usage: List.tryExactlyOne list

Parameters:
    list : 'T list - The input list.

Returns: 'T option The only element of the list or None.

Returns the only element of the list or None if it is empty or contains more than one element.

list : 'T list

The input list.

Returns: 'T option

The only element of the list or None.

Example

 [1] |> List.tryExactlyOne               // evaluates Some 1
 [1;2] |> List.tryExactlyOne             // evaluates None
 ([] : int list) |> List.tryExactlyOne   // evaluates None

List.tryFind predicate list

Full Usage: List.tryFind predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: 'T option The first element for which the predicate returns true, or None if every element evaluates to false.

Returns the first element for which the given function returns True. Return None if no such element exists.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: 'T option

The first element for which the predicate returns true, or None if every element evaluates to false.

Example

Try to find the first even number:

 let inputs = [1; 2; 3]

 inputs |> List.tryFind (fun elm -> elm % 2 = 0)
Evaluates to Some 2

Example

Try to find the first even number:

 let inputs = [1; 5; 3]

 inputs |> List.tryFind (fun elm -> elm % 2 = 0)
Evaluates to None

List.tryFindBack predicate list

Full Usage: List.tryFindBack predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: 'T option The last element for which the predicate returns true, or None if every element evaluates to false.

Returns the last element for which the given function returns True. Return None if no such element exists.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: 'T option

The last element for which the predicate returns true, or None if every element evaluates to false.

Example

Try to find the first even number from the back:

 let inputs = [1; 2; 3; 4; 5]

 inputs |> List.tryFindBack (fun elm -> elm % 2 = 0)
Evaluates to Some 4

Example

Try to find the first even number from the back:

 let inputs = [1; 5; 3]

 inputs |> List.tryFindBack (fun elm -> elm % 2 = 0)
Evaluates to None

List.tryFindIndex predicate list

Full Usage: List.tryFindIndex predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: int option The index of the first element for which the predicate returns true, or None if every element evaluates to false.

Returns the index of the first element in the list that satisfies the given predicate. Return None if no such element exists.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: int option

The index of the first element for which the predicate returns true, or None if every element evaluates to false.

Example

Try to find the index of the first even number:

 let inputs = [1; 2; 3; 4; 5]

 inputs |> List.tryFindIndex (fun elm -> elm % 2 = 0)
Evaluates to Some 1

Example

Try to find the index of the first even number:

 let inputs = [1; 3; 5; 7]

 inputs |> List.tryFindIndex (fun elm -> elm % 2 = 0)
Evaluates to None

List.tryFindIndexBack predicate list

Full Usage: List.tryFindIndexBack predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: int option The index of the last element for which the predicate returns true, or None if every element evaluates to false.

Returns the index of the last element in the list that satisfies the given predicate. Return None if no such element exists.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: int option

The index of the last element for which the predicate returns true, or None if every element evaluates to false.

Example

Try to find the index of the first even number from the back:

 let inputs = [1; 2; 3; 4; 5]

 inputs |> List.tryFindIndexBack (fun elm -> elm % 2 = 0)
Evaluates to Some 3

Example

Try to find the index of the first even number from the back:

 let inputs = [1; 3; 5; 7]

 inputs |> List.tryFindIndexBack (fun elm -> elm % 2 = 0)
Evaluates to None

List.tryHead list

Full Usage: List.tryHead list

Parameters:
    list : 'T list - The input list.

Returns: 'T option The first element of the list or None.

Returns the first element of the list, or None if the list is empty.

list : 'T list

The input list.

Returns: 'T option

The first element of the list or None.

Example

 let inputs = [ "banana"; "pear" ]

 inputs |> List.tryHead
Evaluates to Some "banana"

Example

 let inputs : int list = []

 inputs |> List.tryHead
Evaluates to None

List.tryItem index list

Full Usage: List.tryItem index list

Parameters:
    index : int - The index to retrieve.
    list : 'T list - The input list.

Returns: 'T option The value at the given index or None.

Tries to find the nth element in the list. Returns None if index is negative or the list does not contain enough elements.

index : int

The index to retrieve.

list : 'T list

The input list.

Returns: 'T option

The value at the given index or None.

Example

 let inputs = ["a"; "b"; "c"]

 inputs |> List.tryItem 1
Evaluates to Some "b".

Example

 let inputs = ["a"; "b"; "c"]

 inputs |> List.tryItem 4
Evaluates to None.

List.tryLast list

Full Usage: List.tryLast list

Parameters:
    list : 'T list - The input list.

Returns: 'T option The last element of the list or None.

Returns the last element of the list. Return None if no such element exists.

list : 'T list

The input list.

Returns: 'T option

The last element of the list or None.

Example

 [ "pear"; "banana" ] |> List.tryLast
Evaluates to Some "banana"

Example

 [ ] |> List.tryLast
Evaluates to None

List.tryPick chooser list

Full Usage: List.tryPick chooser list

Parameters:
    chooser : 'T -> 'U option - The function to generate options from the elements.
    list : 'T list - The input list.

Returns: 'U option The first resulting value or None.

Applies the given function to successive elements, returning Some(x) the first result where function returns Some(x) for some x. If no such element exists then return None.

chooser : 'T -> 'U option

The function to generate options from the elements.

list : 'T list

The input list.

Returns: 'U option

The first resulting value or None.

Example

 let input = [1; 2; 3]

 input |> List.tryPick (fun n -> if n % 2 = 0 then Some (string n) else None)
Evaluates to Some "2".

Example

 let input = [1; 2; 3]

 input |> List.tryPick (fun n -> if n > 3 = 0 then Some (string n) else None)
Evaluates to None.

List.unfold generator state

Full Usage: List.unfold generator state

Parameters:
    generator : 'State -> ('T * 'State) option - A function that takes in the current state and returns an option tuple of the next element of the list and the next state value.
    state : 'State - The initial state value.

Returns: 'T list The result list.

Returns a list that contains the elements generated by the given computation. The generator is repeatedly called to build the list until it returns `None`. The given initial state argument is passed to the element generator.

generator : 'State -> ('T * 'State) option

A function that takes in the current state and returns an option tuple of the next element of the list and the next state value.

state : 'State

The initial state value.

Returns: 'T list

The result list.

Example

 1 |> List.unfold (fun state -> if state > 100 then None else Some (state, state * 2))
Evaluates to [1; 2; 4; 8; 16; 32; 64]

List.unzip list

Full Usage: List.unzip list

Parameters:
    list : ('T1 * 'T2) list - The input list.

Returns: 'T1 list * 'T2 list Two lists of split elements.

Splits a list of pairs into two lists.

list : ('T1 * 'T2) list

The input list.

Returns: 'T1 list * 'T2 list

Two lists of split elements.

Example

 let inputs = [(1, "one"); (2, "two")]

 let numbers, names = inputs |> List.unzip
Evaluates numbers to [1; 2] and names to ["one"; "two"].

List.unzip3 list

Full Usage: List.unzip3 list

Parameters:
    list : ('T1 * 'T2 * 'T3) list - The input list.

Returns: 'T1 list * 'T2 list * 'T3 list Three lists of split elements.

Splits a list of triples into three lists.

list : ('T1 * 'T2 * 'T3) list

The input list.

Returns: 'T1 list * 'T2 list * 'T3 list

Three lists of split elements.

Example

 let inputs = [(1, "one", "I"); (2, "two", "II")]

 let numbers, names, roman = inputs |> List.unzip3
Evaluates numbers to [1; 2], names to ["one"; "two"] and roman to ["I"; "II"].

List.updateAt index value source

Full Usage: List.updateAt index value source

Parameters:
    index : int - The index of the item to be replaced.
    value : 'T - The new value.
    source : 'T list - The input list.

Returns: 'T list The result list.

Return a new list with the item at a given index set to the new value.

index : int

The index of the item to be replaced.

value : 'T

The new value.

source : 'T list

The input list.

Returns: 'T list

The result list.

ArgumentException Thrown when index is outside 0..source.Length - 1
Example

 let inputs = [ 0; 1; 2 ]

 inputs |> List.updateAt 1 9
Evaluates to [ 0; 9; 2 ].

List.where predicate list

Full Usage: List.where predicate list

Parameters:
    predicate : 'T -> bool - The function to test the input elements.
    list : 'T list - The input list.

Returns: 'T list A list containing only the elements that satisfy the predicate.

Returns a new list containing only the elements of the list for which the given predicate returns "true"

This is identical to List.filter.

predicate : 'T -> bool

The function to test the input elements.

list : 'T list

The input list.

Returns: 'T list

A list containing only the elements that satisfy the predicate.

Example

Select only the even numbers:

 let inputs = [1; 2; 3; 4]

 inputs |> List.where (fun elm -> elm % 2 = 0)
Evaluates to [2; 4]

List.windowed windowSize list

Full Usage: List.windowed windowSize list

Parameters:
    windowSize : int - The number of elements in each window.
    list : 'T list - The input list.

Returns: 'T list list The result list.

Returns a list of sliding windows containing elements drawn from the input list. Each window is returned as a fresh list.

windowSize : int

The number of elements in each window.

list : 'T list

The input list.

Returns: 'T list list

The result list.

ArgumentException Thrown when windowSize is not positive.
Example

 let inputs = [1; 2; 3; 4; 5]

 inputs |> List.windowed 3
Evaluates to [[1; 2; 3]; [2; 3; 4]; [3; 4; 5]]

List.zip list1 list2

Full Usage: List.zip list1 list2

Parameters:
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.

Returns: ('T1 * 'T2) list A single list containing pairs of matching elements from the input lists.

Combines the two lists into a list of pairs. The two lists must have equal lengths.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

Returns: ('T1 * 'T2) list

A single list containing pairs of matching elements from the input lists.

Example

 let numbers = [1; 2]
 let names = ["one"; "two"]

 List.zip numbers names
Evaluates to [(1, "one"); (2, "two")].

List.zip3 list1 list2 list3

Full Usage: List.zip3 list1 list2 list3

Parameters:
    list1 : 'T1 list - The first input list.
    list2 : 'T2 list - The second input list.
    list3 : 'T3 list - The third input list.

Returns: ('T1 * 'T2 * 'T3) list A single list containing triples of matching elements from the input lists.

Combines the three lists into a list of triples. The lists must have equal lengths.

list1 : 'T1 list

The first input list.

list2 : 'T2 list

The second input list.

list3 : 'T3 list

The third input list.

Returns: ('T1 * 'T2 * 'T3) list

A single list containing triples of matching elements from the input lists.

Example

 let numbers = [1; 2]
 let names = ["one"; "two"]
 let roman = ["I"; "II"]

 Array.zip3 numbers names roman
Evaluates to [(1, "one", "I"); (2, "two", "II")].