List-related functions
Defined in: core::lists
len
Get the length of a list.
head
Get the first element of a list. Yields a runtime error if the list is empty.
tail
Get everything but the first element of a list. Yields a runtime error if the list is empty.
cons
Prepend an element to a list.
cons_end
Append an element to the end of a list.
is_empty
Check if a list is empty.
concat
Concatenate two lists.
take
Get the first n elements of a list.
drop
Get everything but the first n elements of a list.
element_at
Get the element at index i in a list.
range
Generate a range of integer numbers from start to end (inclusive).
reverse
Reverse the order of a list.
map
Generate a new list by applying a function to each element of the input list.
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.
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>]
filter
Filter a list by a predicate.
Example
Run this examplefoldl
Fold a function over a list.
Join a list of strings by folding.
Run this examplesort_by_key
Sort a list of elements, using the given key function that maps the element to a quantity.
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>]
sort
Sort a list of quantities in ascending order.
contains
Returns true if the element x is in the list xs.
unique
Remove duplicates from a given list.
intersperse
Add an element between each pair of elements in a list.
sum
Sum all elements of a list.
linspace
Generate a list of n_steps evenly spaced numbers from start to end (inclusive).
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>]
join
Convert a list of strings into a single string by concatenating them with a separator.
split
Split a string into a list of strings using a separator.
Example
split("Numbat is a statically typed programming language.", " ")
= ["Numbat", "is", "a", "statically", "typed", "programming", "language."] [List<String>]