List
Purely-functional, singly-linked lists.
List
type List<T> = ?(T, List<T>)
nil
func nil<T>() : List<T>
Create an empty list.
isNil
func isNil<T>(l : List<T>) : Bool
Check whether a list is empty and return true if the list is empty.
last
func last<T>(l : List<T>) : ?T
Return the last element of the list, if present.
size
func size<T>(l : List<T>) : Nat
Return the length of the list.
get
func get<T>(l : List<T>, n : Nat) : ?T
Access any item in a list, zero-based.
Indexing into a list is a linear operation, and usually an indication that a list might not be the best data structure to use. |
iterate
func iterate<T>(l : List<T>, f : T -> ())
Call the given function with each list element in turn.
This function is equivalent to the app
function in Standard ML Basis,
and the iter
function in OCaml.
partition
Create two new lists from the results of a given function (f
).
The first list only includes the elements for which the given
function f
returns true and the second list only includes
the elements for which the function returns false.
mapResult
func mapResult<A, R, E>(xs : List<A>, f : A -> Result.Result<R, E>) : Result.Result<List<R>, E>
Maps a Result-returning function over a List and returns either the first error or a list of successful values.
foldLeft
func foldLeft<T, S>(l : List<T>, a : S, f : (S, T) -> S) : S
Fold the list left-to-right using the given function (f
).
foldRight
func foldRight<T, S>(l : List<T>, a : S, f : (T, S) -> S) : S
Fold the list right-to-left using the given function (f
).
find
func find<T>(l : List<T>, f : T -> Bool) : ?T
Return the first element for which the given predicate f
is true,
if such an element exists.
some
func some<T>(l : List<T>, f : T -> Bool) : Bool
Return true if there exists a list element for which
the given predicate f
is true.
all
func all<T>(l : List<T>, f : T -> Bool) : Bool
Return true if the given predicate f
is true for all list
elements.
compare
func compare<T>(l1 : List<T>, l2 : List<T>, compElm : (T, T) -> Order.Order) : Order.Order
Compare two lists using lexicographic ordering specified by the given relation lte
.
tabulate
func tabulate<T>(n : Nat, f : Nat -> T) : List<T>
Generate a list based on a length and a function that maps from a list index to a list element.
make
func make<X>(x : X) : List<X>
Create a list with exactly one element.
replicate
func replicate<X>(n : Nat, x : X) : List<X>
Create a list of the given length with the same value in each position.
zipWith
Create a list in which elements are calculated from the function f
and
include elements occuring at the same position in the given lists.
If the given lists have different lengths, then the created list will have a length equal to the length of the smaller list.
fromArray
func fromArray<A>(xs : [A]) : List<A>
Convert an array into a list.
fromVarArray
func fromVarArray<A>(xs : [var A]) : List<A>
Convert a mutable array into a list.
toArray
func toArray<A>(xs : List<A>) : [A]
Create an array from a list.
toVarArray
func toVarArray<A>(xs : List<A>) : [var A]
Create a mutable array from a list.