List
Functions
map

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

Type Signature
((a) -> b, list<a, n>) -> list<b, n>
Parameters
f : (a) -> b

The mapping function

lst : list<t, n>

The list to map.

Returns

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

fold

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

Type Signature
((t, state) -> state, state, list<t, n>) -> state
Parameters
f : (t, state) -> state

The function to update the state given the input elements.

initState : state

The initial state.

lst : list<t, n>

The input list.

Returns

The final state value.

foldBack

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

Type Signature
((t, state) -> state, state, list<t, n>) -> state
Parameters
f : (t,state) -> state

The function to update the state given the input elements.

initState : state

The initial state.

lst : list<t, n>

The input list.

Returns

The final state value.

reduce

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

Type Signature
((t, t) -> t, list<t, n>) -> t
Parameters
f : (t, t) -> t

The function to update the accumulator, given an element from the list as the first parameter, and the accumulator as the second parameter.

lst : list<t, n>

The list to reduce over.

Returns

The final accumulated value.

tryReduce

Applies a function f to each element of the list in order, threading an accumulator argument through the computation. The initial accumulator is the first element of the list if the length is greater than zero. If the list has length 0, nothing() is returned.

Type Signature
((t, t) -> t, list<t, n>) -> maybe<t>
Parameters
f : (t, t) -> t

The function to update the accumulator, given an element from the list as the first parameter, and the accumulator as the second parameter.

lst : list<t, n>

The list to reduce over.

Returns

The final accumulated value if the length was greater than 0, and nothing() otherwise.

reduceBack

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

Type Signature
((t, t) -> t, list<t, n>) -> t
Parameters
f : (t, t) -> t

The function to update the accumulator, given an element from the list as the first parameter, and the accumulator as the second parameter.

lst : list<t, n>

The list to reduce over.

Returns

The final accumulated value.

tryReduceBack

Applies a function f to each element of the list in reverse order, threading an accumulator argument through the computation. The initial accumulator is the last element of the list if the length is greater than zero. If the list has length 0, nothing() is returned.

Type Signature
((t, t) -> t, list<t, n>) -> maybe<t>
Parameters
f : (t, t) -> t

The function to update the accumulator, given an element from the list as the first parameter, and the accumulator as the second parameter.

lst : list<t, n>

The list to reduce over.

Returns

The final accumulated value if the length was greater than 0, and nothing() otherwise.

concat

Concatenates two lists.

Type Signature
(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.

concatSafe

Concatenates two lists, always allocating enough space in the return list to hold the items.

Type Signature
(list<t, aCap>, list<t, bCap>) -> list<t, aCap + bCap>
Parameters
lstA : list<t, aCap>

The first list.

lstB : list<t, bCap>

The second list.

Returns

The two lists appended together.

get

Returns the nth element of the given list

Type Signature
(u, list<t, n>) -> t where u : int
Parameters
i : u

The index of the element to be retrieved

lst : list<t, n>

The given list

Returns

The element at position i in the list

tryGet

Returns the nth element of the given list, or nothing if the index is out of bounds

Type Signature
(uint32, list<t, n>) -> maybe<t>
Parameters
i : uint32

The index of the element to be retrieved

lst : list<t, n>

The given list

Returns

The element at position i in the list inside a just, or nothing if the index is out of bounds.

flatten

Flattens a list of lists into a single list.

Type Signature
(list<list<t, m>, n>) -> list<t, retCap>
Parameters
listOfLists : list<list<t, m>, n>

The list of lists

Returns

The flattened list

flattenSafe

Flattens a list of lists into a single list.

Type Signature
(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. Note that you can specify the output capacity m by using a type constraint at the call site.

Type Signature
(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) -> bool, list<t, n>) -> bool
Parameters
pred : (t) -> bool

The 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) -> bool, list<t, n>) -> bool
Parameters
pred : (t) -> bool

The 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.

append

Mutates the given list with the element appended to the end.  If the element will not fit in the list, nothing is inserted.

Type Signature
(t, inout list<t, n>) -> list<t, n>
Parameters
elem : t

The element to add to the end of the list

lst : inout list<t, n>

The list

Returns

A copy of lst with elem appended to the end

appendPure

Returns a copy of the given list with the element appended to the end.  If the element will not fit in the list, nothing is inserted.

Type Signature
(t, list<t, n>) -> list<t, n>
Parameters
elem : t

The 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

appendSafe

Returns a copy of the given list with the element appended to the end.

Type Signature
(t, list<t, n>) -> list<t, n + 1>
Parameters
elem : t

The 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

prepend

Mutates a list, adding and element 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, inout list<t, n>) -> unit
Parameters
elem : t

The element to add to the front of the list

lst : inout list<t, n>

The list

Returns

unit

prependPure

Returns a copy of the given list with the element prepended 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, list<t, n>) -> list<t, n>
Parameters
elem : t

The element to add to the front of the list

lst : list<t, n>

The list

Returns

A copy of lst with elem prepended to the front

set

Mutates the given list where the element at the given index is the given element.  If the index is out of bounds the list remains unchanged.

Type Signature
(uint32, t, inout list<t, n>) -> unit
Parameters
index : uint32

The index to set

elem : t

The new element

lst : inout list<t, n>

The list

Returns

unit

setPure

Returns a copy of the given list where the element at the given index is the given element. If the index is out of bounds the list remains unchanged.

Type Signature
(uint32, t, list<t, n>) -> list<t, n>
Parameters
index : uint32

The index to set

elem : t

The 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
(uint32, t) -> list<t, n>
Parameters
numOfElements : uint32

The length of the new list

elem : t

The element to replicate

Returns

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

remove

Mutates the input list where the first element equal to elem is deleted, if there is such an element.

Type Signature
(t, list<t, n>) -> unit
Parameters
elem : t

The element to remove from the list

lst : t

The list to mutate

Returns

unit

removePure

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

Type Signature
(t, list<t, n>) -> list<t, n>
Parameters
elem : t

The element to remove from the list

lst : t

The list

Returns

A copy of lst with elem removed.

pop

Mutates and drops the last element of a List. If the list has size 0, the list remains unchanged

Type Signature
(inout list<t, n>) -> unit
Parameters
lst : list<t, n>

The list

Returns

unit

popPure

Drops the last element of a List in a copy. If the list has size 0, the list remains unchanged

Type Signature
(list<t, n>) -> list<t, n>
Parameters
lst : list<t, n>

The list

Returns

The list with the last element dropped

iter

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) -> unit, list<t, n>) -> unit
Parameters
f : (t) -> unit

The function to call

lst : list<t, n>

The list

Returns

Unit

last

Returns the last element of the lst.

Type Signature
(list<t, n>) -> t
Parameters
lst : list<t, n>

The list

Returns

The last element of the list

tryLast

Returns the last element of the lst.

Type Signature
(list<t, n>) -> maybe<t>
Parameters
lst : list<t, n>

The list

Returns

The last element of the list if the length is greater than 0, or nothing otherwise.

tryMax

Returns the maximum element of the lst. If the list is empty, nothing() is returned.

Type Signature
(list<t, n>) -> maybe<t> where t : num
Parameters
lst : list<t, n>

The list

Returns

The maximum element of the list

tryMin

Returns the minimum element of the lst. If the list is empty, nothing() is returned.

Type Signature
(list<t, n>) -> maybe<t> where t : num
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, list<t, n>) -> bool
Parameters
elem : t

The 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 & the second element is taken from corresponding element in the second list. If the lists are not of equal length, the elements from the longer list are dropped.

Type Signature
(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, & the second list contains the second element of each tuple.

Type Signature
(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
(list<a, n>) -> a where a : num
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
(list<a, n>) -> a where a : num
Parameters
lst : list<a, n>

The list to average

Returns

The numerical mean of the list

sort

Sorts a list in place using the heapsort algorithm. The heapsort algorithm was chosen since it uses no recursion, making it nice for embedded systems, and it still runs in O(n log n) time.

Type Signature
((t) -> m, inout list<t, n>) -> unit where m : num
Parameters
key : (t) -> m

The key function to use for sorting. Elements will be ordered according to what is returned by the key function.

lst : inout list<t, n>

The list to sort in place.

Returns

unit

sorted

Creates a new list that is sorted using the heapsort algorithm.

Type Signature
((t) -> m, list<t, n>) -> list<t, n> where m : num
Parameters
key : (t) -> m

The key function to use for sorting. Elements will be ordered according to what is returned by the key function.

lst : list<t, n>

The list to sort.

Returns

The sorted list.