Calculemus


List Module

Handy functions for list manipulation.

Functions and values

Function or value Description

allpairs f l1 l2

Full Usage: allpairs f l1 l2

Parameters:
    f : 'a -> 'b -> 'c - The function to apply to the pairs.
    l1 : 'a list - The list of first elements of the pairs.
    l2 : 'b list - The list of second elements of the pairs.

Returns: 'c list The list of results of applying f to all the pairs formed from l1 and l2.

Computes the list of all results from applying the function f to pairs from the two input lists l1 and l2.

allpairs f [x1;...;xm] [y1;...;yn] returns the list of results [f x1 y1; f x1 y2; ... ; f x1 yn; f x2 y1 ; f x2 y2 ...; f xm y1; f xm y2 ...; f xm yn]

f : 'a -> 'b -> 'c

The function to apply to the pairs.

l1 : 'a list

The list of first elements of the pairs.

l2 : 'b list

The list of second elements of the pairs.

Returns: 'c list

The list of results of applying f to all the pairs formed from l1 and l2.

Example

 allpairs (+) [1;2;3] [1;2]
 // [1+1; 1+2; 2+1; 2+2; 3+1; 3+2]
Evaluates to [2; 3; 3; 4; 4; 5].

assoc a l

Full Usage: assoc a l

Parameters:
    a : 'a - The value to search.
    l : ('a * 'b) list - The input list.

Returns: 'b The second component of the pair, if a matching for the first is found.

Searches a list of pairs l for a pair whose first component equals a specified value a, failing if no matching is found.

assoc a [(a1,b1);...;(an,bn)] returns the first bi in the list such that ai equals a.

a : 'a

The value to search.

l : ('a * 'b) list

The input list.

Returns: 'b

The second component of the pair, if a matching for the first is found.

Exception Thrown with message find when no matching is found.
Example

 assoc 2 [(1,2);(2,3)]
Evaluates to 3.

Example

 assoc 2 [(1,2);(2,3);(2,4)]
Evaluates to 3.

Example

 assoc 3 [(1,2);(2,3)]
Throws System.Exception: find.

butlast l

Full Usage: butlast l

Parameters:
    l : 'a list - The input list.

Returns: 'a list All the elements of the input list but the last one.

Computes the sub-list of a list consisting of all but the last element.

l : 'a list

The input list.

Returns: 'a list

All the elements of the input list but the last one.

Exception Thrown with message butlast when the input does not have any elements.
Example

 butlast [1;2;3;4;5]
Evaluates to [1;2;3;4].

Example

 butlast ([]:int list)
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
type 'T list = List<'T>
Throws System.Exception: butlast.

chop_list n l

Full Usage: chop_list n l

Parameters:
    n : int - The index at which the list is chopped.
    l : 'a list - The input list.

Returns: 'a list * 'a list The two chopped lists.

Chops a list l into two parts at the specified index n.

chop_list i [x1;...;xn] returns ([x0;...;xi-1],[xi;...;xn]).

n : int

The index at which the list is chopped.

l : 'a list

The input list.

Returns: 'a list * 'a list

The two chopped lists.

Exception Thrown with message chop_list when chop index is negative or exceeds the number of elements in the list.
Note

It should be replaced with the standard ListModule.SplitAt.

Example

 chop_list 3 [1;2;3;4;5;6]
Evaluates to ([1;2;3], [4;5;6]).

Example

 ['a';'b';'c';'d'] |> chop_list -1
Throws System.Exception: chop_list.

Example

 ['a';'b';'c';'d'] |> chop_list 5
Throws System.Exception: chop_list.

distinctpairs l

Full Usage: distinctpairs l

Parameters:
    l : 'a list - The input list.

Returns: ('a * 'a) list All pairs of distinct elements from the input list l.

Produces all pairs of distinct elements from a single list.

l : 'a list

The input list.

Returns: ('a * 'a) list

All pairs of distinct elements from the input list l.

Example

 distinctpairs [1;2;3;4]
Evaluates to [(1, 2); (1, 3); (1, 4); (2, 3); (2, 4); (3, 4)].

earlier l x y

Full Usage: earlier l x y

Parameters:
    l : 'a list - The input list.
    x : 'a - The element to search.
    y : 'a - The element to compare with x.

Returns: bool true, if x comes earlier than y in l, or x is in l but not y. Otherwise, false.

Checks if x comes earlier than y in list l.

l : 'a list

The input list.

x : 'a

The element to search.

y : 'a

The element to compare with x.

Returns: bool

true, if x comes earlier than y in l, or x is in l but not y. Otherwise, false.

Example

 earlier [0;1;2;3] 2 3
Evaluates to true.

Example

 earlier [0;1;2;3] 3 4
Evaluates to true.

Example

 earlier [0;1;2;3] 3 2
Evaluates to false.

Example

 earlier [0;1;3] 2 3
Evaluates to false.

Example

 earlier [0;1;2;3] 4 5
Evaluates to false.

index x xs

Full Usage: index x xs

Parameters:
    x : 'a - The element to search.
    xs : 'a list - The input list.

Returns: int The index of the first element that equals x.
Modifiers: inline
Type parameters: 'a

Returns the index of the first element in the list xs that equals x. Raises KeyNotFoundException if no such element exists.

x : 'a

The element to search.

xs : 'a list

The input list.

Returns: int

The index of the first element that equals x.

KeyNotFoundException Thrown when no element in the list equals x.
Example

 index 2 [0;1;3;3;2;3]
Evaluates to 4.

Example

 index 5 [0;1;3;3;2;3]
Throws System.Collections.Generic.KeyNotFoundException: An index satisfying the predicate was not found in the collection..

insertat i x l

Full Usage: insertat i x l

Parameters:
    i : int - The index where the item should be inserted.
    x : 'a - The value to insert.
    l : 'a list - The input list.

Returns: 'a list The result list.

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

i : int

The index where the item should be inserted.

x : 'a

The value to insert.

l : 'a list

The input list.

Returns: 'a list

The result list.

Exception Thrown when index is below 0 or greater than l.Length.
Note

It should be replaced with the standard ListModule.InsertAt.

Example

 [ 0; 1; 2 ] |> insertat 1 9
Evaluates to [0; 9; 1; 2].

Example

 [ 0; 1; 2 ] |> insertat -1 9
Throws System.Exception: insertat: list too short for position to exist.

Example

 [ 0; 1; 2 ] |> insertat -1 4
Throws System.Exception: insertat: list too short for position to exist.

last l

Full Usage: last l

Parameters:
    l : 'a list - The input list.

Returns: 'a The last element of the list.

Returns the last element of the list.

l : 'a list

The input list.

Returns: 'a

The last element of the list.

Exception Thrown with message last when the input does not have any elements.
Note

It should be replaced with the standard ListModule.Last.

Example

 last [1;2;3;4;5]
Evaluates to 5.

Example

 last ([]:int list)
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
type 'T list = List<'T>
Throws System.Exception: last.

rev_assoc b l

Full Usage: rev_assoc b l

Parameters:
    b : 'b - The value to search.
    l : ('a * 'b) list - The input list.

Returns: 'a The first component of the pair, if a matching for the second is found.

Searches a list of pairs l for a pair whose second component equals a specified value b, failing if no matching is found.

rev_assoc b [(a1,b1);...;(an,bn)] returns the first ai in the list such that bi equals b.

b : 'b

The value to search.

l : ('a * 'b) list

The input list.

Returns: 'a

The first component of the pair, if a matching for the second is found.

Exception Thrown with message find when no matching is found.
Example

 rev_assoc 2 [(1,2);(2,3)]
Evaluates to 1.

Example

 rev_assoc 2 [(1,2);(2,2);(2,3)]
Evaluates to 1.

Example

 rev_assoc 1 [(1,2);(2,3)]
Throws System.Exception: find.