List

Summary
List
Functions
mapApplies f to every element of the list and returns the new list.
foldlApplies a function f to each element of the list, threading an accumulator argument through the computation.
foldrApplies a function f to each element of the list in reverse order, threading an accumulator argument through the computation.
appendAppends two lists.
nthReturns the nth element of the given list
flattenSafeFlattens a list of lists into a single list.
resizeCreates a copy of the list which has a new capacity m.
allReturns true if pred(elem) returns true for all elements elem in lst, otherwise false.
anyReturns true if pred(elem) returns true for at least one element elem in lst, otherwise false.
pushBackReturns a copy of the given list with the element appended to the end.
pushOffFrontReturns a copy of the given list with the element appended to the front.
setNthReturns a copy of the given list where the element at the given index is the given element.
replicateInitializes a new list of a given length using the given element.
removeReturns a copy of lst where the first element equal to elem is deleted, if there is such an element.
dropLastDrops the last element of a List.
foreachCalls f for each element in lst.
lastReturns the last element of the lst.
max_Returns the maximum element of the lst.
min_Returns the minimum element of the lst.
memberReturns true if elem is a member of lst, otherwise false.
zipZips two lists of equal length into one list of two-tuples, where the first element of each tuple is taken from the first list and the second element is taken from corresponding element in the second list.
unzipUnzips a list of two-tuples into two lists, where the first list contains the first element of each tuple, and the second list contains the second element of each tuple.
sumComputes the sum of a list of numbers
averageComputes the average (mean) of a list of numbers

Functions

map

Applies f to every element of the list and returns the new list.

Type Signature

<'a,'b;n>(('a) -> 'b, list<'a; n>) -> list<'b; n>

Parameters

f : (‘a) -> ‘bThe mapping function
lst : list<’t;aCap>The list to map.

Returns

The list obtained by applying f to every element of lst.

foldl

Applies a function f to each element of the list, threading an accumulator argument through the computation.

Type Signature

<'t,'state;n>(('t, 'state) -> 'state, 'state, list<'t; n>) -> 'state

Parameters

f : (‘t, ‘state) -> ‘stateThe function to update the state given the input elements.
initState : ‘stateThe initial state.
lst : list<’t; n>The input list.

Returns

The final state value.

foldr

Applies a function f to each element of the list in reverse order, threading an accumulator argument through the computation.

Type Signature

<'t,'state;n>(('t, 'state) -> 'state, 'state, list<'t; n>) -> 'state

Parameters

f : (‘t,’state) -> ‘stateThe function to update the state given the input elements.
initState : ‘stateThe initial state.
lst : list<’t; n>The input list.

Returns

The final state value.

append

Appends two lists.

Type Signature

<'t;aCap,bCap,retCap>(list<'t;aCap>, list<'t;bCap>) -> list<'t;retCap>

Parameters

lstA : list<’t;aCap>The first list.
lstB : list<’t;bCap>The second list.

Returns

The two lists appended together.

nth

Returns the nth element of the given list

Type Signature

<'t;n>(uint32, list<'t;n>) -> 't

Parameters

i : uint32The index of the element to be retrieved
lst : list<’t;n>The given list

Returns

The element at position i in the list

flattenSafe

Flattens a list of lists into a single list.

Type Signature

<'t;m,n>(list<list<'t;m>;n>) -> list<'t;m*n>

Parameters

listOfLists : list<list<’t;m>;n>The list of lists

Returns

The flattened list

resize

Creates a copy of the list which has a new capacity m.  The contents and length of the list remained unchanged.

Type Signature

<'t;n,m>(list<'t;n>) -> list<'t;m>

Parameters

lst : list<’t;n>The list to resize

Returns

The new list with capacity m

all

Returns true if pred(elem) returns true for all elements elem in lst, otherwise false.

Type Signature

<'t;n>(('t) -> bool, list<'t;n>) -> bool

Parameters

pred : (‘t) -> boolThe predicate which must be satisfied
lst : list<’t;n>The list on which the predicate is checked

Returns

A boolean value which determines if pred satsified for every element of lst.

any

Returns true if pred(elem) returns true for at least one element elem in lst, otherwise false.

Type Signature

<'t;n>(('t) -> bool, list<'t;n>) -> bool

Parameters

pred : (‘t) -> boolThe predicate which must be satisfied
lst : list<’t;n>The list on which the predicate is checked

Returns

A boolean value which determines if pred is satsified for some element of lst.

pushBack

Returns a copy of the given list with the element appended to the end.  If the element will not fit in the list, the program crashes.

Type Signature

<'t;n>('t, list<'t;n>) -> list<'t;n>

Parameters

elem : ‘tThe element to add to the end of the list
lst : list<’t;n>The list

Returns

A copy of lst with elem appended to the end

pushOffFront

Returns a copy of the given list with the element appended to the front.  If the list is already full then the last element is pushed off (removed) from the back of the list.

Type Signature

<'t;n>('t, list<'t;n>) -> list<'t;n>

Parameters

elem : ‘tThe element to add to the front of the list
lst : list<’t;n>The list

Returns

A copy of lst with elem appended to the front

setNth

Returns a copy of the given list where the element at the given index is the given element.

Type Signature

<'t;n>(uint32, 't, list<'t;n>) -> list<'t;n>

Parameters

index : uint32The index to set
elem : ‘tThe new element
lst : list<’t;n>The list

Returns

A new list where the elem is at index.

replicate

Initializes a new list of a given length using the given element.

Type Signature

<'t;n>(uint32, 't) -> list<'t;n>

Parameters

numOfElements : uint32The length of the new list
elem : ‘tThe element to replicate

Returns

A new list with elem at every position in the list.

remove

Returns a copy of lst where the first element equal to elem is deleted, if there is such an element.

Type Signature

<'t;n>('t, list<'t;n>) -> list<'t;n>

Parameters

elem : ‘tThe element to remove from the list
lst : ‘tThe list

Returns

A copy of lst with elem removed.

dropLast

Drops the last element of a List.  The list should be non-empty, otherwise the program will exit.

Type Signature

<'t;n>(list<'t;n>) -> list<'t;n>

Parameters

lst : list<’t;n>The list

Returns

The list with the last element dropped

foreach

Calls f for each element in lst.  This function is used for its side effects and the evaluation order is defined to be the same as the order of the elements in the list.

Type Signature

<'t;n>(('t) -> unit, list<'t;n>) -> unit

Parameters

f : (‘t) -> unitThe function to call
lst : list<’t;n>The list

Returns

Unit

last

Returns the last element of the lst.

Type Signature

<'t;n>(list<'t;n>) -> 't

Parameters

lst : list<’t;n>The list

Returns

The last element of the list

max_

Returns the maximum element of the lst.  The program exits if the list has no elements.

Type Signature

<'t;n>(list<'t;n>) -> 't

Parameters

lst : list<’t;n>The list

Returns

The maximum element of the list

min_

Returns the minimum element of the lst.  The program exits if the list has no elements.

Type Signature

<'t;n>(list<'t;n>) -> 't

Parameters

lst : list<’t;n>The list

Returns

The minimum element of the list

member

Returns true if elem is a member of lst, otherwise false.

Type Signature

<'t;n>('t, list<'t;n>) -> bool

Parameters

elem : ‘tThe element to match
lst : list<’t;n>The list to search

Returns

True if elem is in the list, otherwise false.

zip

Zips two lists of equal length into one list of two-tuples, where the first element of each tuple is taken from the first list and the second element is taken from corresponding element in the second list.  If the lists are not of equal length, the program exits.

Type Signature

<'a,'b;n>(list<'a;n>, list<'b;n>) -> list<('a*'b);n>

Parameters

lstA : list<’a;n>The first list to zip
lstB : list<’b;n>The second list to zip

Returns

The zipped lists.

unzip

Unzips a list of two-tuples into two lists, where the first list contains the first element of each tuple, and the second list contains the second element of each tuple.

Type Signature

<'a,'b;n>(list<('a*'b);n>) -> (list<'a;n>*list<'b;n>)

Parameters

lst : list<(‘a*’b);n>The list to unzip

Returns

The two unzipped lists.

sum

Computes the sum of a list of numbers

Type Signature

<'a;n>(list<'a;n>) -> 'a

Parameters

lst : list<’a;n>The list to sum

Returns

The sum of the list

average

Computes the average (mean) of a list of numbers

Type Signature

<'a;n>(list<'a;n>) -> 'a

Parameters

lst : list<’a;n>The list to average

Returns

The numerical mean of the list

Close