Skip to content

List-related functions

Defined in: core::lists

len

Get the length of a list.

fn len<A>(xs: List<A>) -> Scalar

Example

len([3, 2, 1])

    = 3
Run this example

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

fn head<A>(xs: List<A>) -> A

Example

head([3, 2, 1])

    = 3
Run this example

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>

Example

tail([3, 2, 1])

    = [2, 1]    [List<Scalar>]
Run this example

cons

Prepend an element to a list.

fn cons<A>(x: A, xs: List<A>) -> List<A>

Example

cons(77, [3, 2, 1])

    = [77, 3, 2, 1]    [List<Scalar>]
Run this example

cons_end

Append an element to the end of a list.

fn cons_end<A>(x: A, xs: List<A>) -> List<A>

Example

cons_end(77, [3, 2, 1])

    = [3, 2, 1, 77]    [List<Scalar>]
Run this example

is_empty

Check if a list is empty.

fn is_empty<A>(xs: List<A>) -> Bool

Example

is_empty([3, 2, 1])

    = false    [Bool]
Run this example

Example

is_empty([])

    = true    [Bool]
Run this example

concat

Concatenate two lists.

fn concat<A>(xs1: List<A>, xs2: List<A>) -> List<A>

Example

concat([3, 2, 1], [10, 11])

    = [3, 2, 1, 10, 11]    [List<Scalar>]
Run this example

take

Get the first n elements of a list.

fn take<A>(n: Scalar, xs: List<A>) -> List<A>

Example

take(2, [3, 2, 1, 0])

    = [3, 2]    [List<Scalar>]
Run this example

drop

Get everything but the first n elements of a list.

fn drop<A>(n: Scalar, xs: List<A>) -> List<A>

Example

drop(2, [3, 2, 1, 0])

    = [1, 0]    [List<Scalar>]
Run this example

element_at

Get the element at index i in a list.

fn element_at<A>(i: Scalar, xs: List<A>) -> A

Example

element_at(2, [3, 2, 1, 0])

    = 1
Run this example

range

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

fn range(start: Scalar, end: Scalar) -> List<Scalar>

Example

range(2, 12)

    = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]    [List<Scalar>]
Run this example

reverse

Reverse the order of a list.

fn reverse<A>(xs: List<A>) -> List<A>

Example

reverse([3, 2, 1])

    = [1, 2, 3]    [List<Scalar>]
Run this example

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>

Square all elements of a list.

map(sqr, [3, 2, 1])

    = [9, 4, 1]    [List<Scalar>]
Run this example

map2

Generate a new list by applying a function to each element of the input list. This function takes two inputs: a variable, and the element of the list.

fn map2<A, B, C>(f: Fn[(A, B) -> C], other: A, xs: List<B>) -> List<C>

Returns a list of bools corresponding to whether the sublist contains a 2 or not.

map2(contains, 2, [[0], [2], [1, 2], [0, 2, 3], []])

    = [false, true, true, true, false]    [List<Bool>]
Run this example

filter

Filter a list by a predicate.

fn filter<A>(p: Fn[(A) -> Bool], xs: List<A>) -> List<A>

Example

filter(is_finite, [0, 1e10, NaN, -inf])

    = [0, 10_000_000_000]    [List<Scalar>]
Run this example

foldl

Fold a function over a list.

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

Join a list of strings by folding.

foldl(str_append, "", ["Num", "bat", "!"])

    = "Numbat!"    [String]
Run this example

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>

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<Scalar>]
Run this example

sort

Sort a list of quantities in ascending order.

fn sort<D: Dim>(xs: List<D>) -> List<D>

Example

sort([3, 2, 7, 8, -4, 0, -5])

    = [-5, -4, 0, 2, 3, 7, 8]    [List<Scalar>]
Run this example

contains

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

fn contains<A>(x: A, xs: List<A>) -> Bool

Example

[3, 2, 7, 8, -4, 0, -5] |> contains(0)

    = true    [Bool]
Run this example

Example

[3, 2, 7, 8, -4, 0, -5] |> contains(1)

    = false    [Bool]
Run this example

unique

Remove duplicates from a given list.

fn unique<A>(xs: List<A>) -> List<A>

Example

unique([1, 2, 2, 3, 3, 3])

    = [1, 2, 3]    [List<Scalar>]
Run this example

intersperse

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

fn intersperse<A>(sep: A, xs: List<A>) -> List<A>

Example

intersperse(0, [1, 1, 1, 1])

    = [1, 0, 1, 0, 1, 0, 1]    [List<Scalar>]
Run this example

sum

Sum all elements of a list.

fn sum<D: Dim>(xs: List<D>) -> D

Example

sum([3 m, 200 cm, 1000 mm])

    = 6 m    [Length]
Run this example

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>

Example

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<Length>]
Run this example

join

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

fn join(xs: List<String>, sep: String) -> String

Example

join(["snake", "case"], "_")

    = "snake_case"    [String]
Run this example

split

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

fn split(input: String, separator: String) -> List<String>

Example

split("Numbat is a statically typed programming language.", " ")

    = ["Numbat", "is", "a", "statically", "typed", "programming", "language."]    [List<String>]
Run this example