List-related functions

Defined in: core::lists

len

Get the length of a list.

fn len<A>(xs: List<A>) -> Scalar
Examples
len([3, 2, 1]) = 3

Get the first element of a list. Yields a runtime error if the list is empty.

fn head<A>(xs: List<A>) -> A
Examples
head([3, 2, 1]) = 3

tail

Get everything but the first element of a list. Yields a runtime error if the list is empty.

fn tail<A>(xs: List<A>) -> List<A>
Examples
tail([3, 2, 1]) = [2, 1] [List]

cons

Prepend an element to a list.

fn cons<A>(x: A, xs: List<A>) -> List<A>
Examples
cons(77, [3, 2, 1]) = [77, 3, 2, 1] [List]

cons_end

Append an element to the end of a list.

fn cons_end<A>(x: A, xs: List<A>) -> List<A>
Examples
cons_end(77, [3, 2, 1]) = [3, 2, 1, 77] [List]

is_empty

Check if a list is empty.

fn is_empty<A>(xs: List<A>) -> Bool
Examples
is_empty([3, 2, 1]) = false [Bool]
is_empty([]) = true [Bool]

concat

Concatenate two lists.

fn concat<A>(xs1: List<A>, xs2: List<A>) -> List<A>
Examples
concat([3, 2, 1], [10, 11]) = [3, 2, 1, 10, 11] [List]

take

Get the first n elements of a list.

fn take<A>(n: Scalar, xs: List<A>) -> List<A>
Examples
take(2, [3, 2, 1, 0]) = [3, 2] [List]

drop

Get everything but the first n elements of a list.

fn drop<A>(n: Scalar, xs: List<A>) -> List<A>
Examples
drop(2, [3, 2, 1, 0]) = [1, 0] [List]

element_at

Get the element at index i in a list.

fn element_at<A>(i: Scalar, xs: List<A>) -> A
Examples
element_at(2, [3, 2, 1, 0]) = 1

range

Generate a range of integer numbers from start to end (inclusive).

fn range(start: Scalar, end: Scalar) -> List<Scalar>
Examples
range(2, 12) = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [List]

reverse

Reverse the order of a list.

fn reverse<A>(xs: List<A>) -> List<A>
Examples
reverse([3, 2, 1]) = [1, 2, 3] [List]

map

Generate a new list by applying a function to each element of the input list.

fn map<A, B>(f: Fn[(A) -> B], xs: List<A>) -> List<B>
Examples

Square all elements of a list.

map(sqr, [3, 2, 1]) = [9, 4, 1] [List]

filter

Filter a list by a predicate.

fn filter<A>(p: Fn[(A) -> Bool], xs: List<A>) -> List<A>
Examples
filter(is_finite, [0, 1e10, NaN, -inf]) = [0, 10_000_000_000] [List]

foldl

Fold a function over a list.

fn foldl<A, B>(f: Fn[(A, B) -> A], acc: A, xs: List<B>) -> A
Examples

Join a list of strings by folding.

foldl(str_append, "", ["Num", "bat", "!"]) = "Numbat!" [String]

sort_by_key

Sort a list of elements, using the given key function that maps the element to a quantity.

fn sort_by_key<A, D: Dim>(key: Fn[(A) -> D], xs: List<A>) -> List<A>
Examples

Sort by last digit.

fn last_digit(x) = mod(x, 10) sort_by_key(last_digit, [701, 313, 9999, 4]) = [701, 313, 4, 9999] [List]

sort

Sort a list of quantities in ascending order.

fn sort<D: Dim>(xs: List<D>) -> List<D>
Examples
sort([3, 2, 7, 8, -4, 0, -5]) = [-5, -4, 0, 2, 3, 7, 8] [List]

contains

Returns true if the element x is in the list xs.

fn contains<A>(x: A, xs: List<A>) -> Bool
Examples
[3, 2, 7, 8, -4, 0, -5] |> contains(0) = true [Bool]
[3, 2, 7, 8, -4, 0, -5] |> contains(1) = false [Bool]

unique

Remove duplicates from a given list.

fn unique<A>(xs: List<A>) -> List<A>
Examples
unique([1, 2, 2, 3, 3, 3]) = [1, 2, 3] [List]

intersperse

Add an element between each pair of elements in a list.

fn intersperse<A>(sep: A, xs: List<A>) -> List<A>
Examples
intersperse(0, [1, 1, 1, 1]) = [1, 0, 1, 0, 1, 0, 1] [List]

sum

Sum all elements of a list.

fn sum<D: Dim>(xs: List<D>) -> D
Examples
sum([3 m, 200 cm, 1000 mm]) = 6 m [Length]

linspace

Generate a list of n_steps evenly spaced numbers from start to end (inclusive).

fn linspace<D: Dim>(start: D, end: D, n_steps: Scalar) -> List<D>
Examples
linspace(-5 m, 5 m, 11) = [-5 m, -4 m, -3 m, -2 m, -1 m, 0 m, 1 m, 2 m, 3 m, 4 m, 5 m] [List]

join

Convert a list of strings into a single string by concatenating them with a separator.

fn join(xs: List<String>, sep: String) -> String
Examples
join(["snake", "case"], "_") = "snake_case" [String]

split

Split a string into a list of strings using a separator.

fn split(input: String, separator: String) -> List<String>
Examples
split("Numbat is a statically typed programming language.", " ") = ["Numbat", "is", "a", "statically", "typed", "programming", "language."] [List]