Skip to content

std/lists

Operations on the persistent linked List<T> type. Container-specific ops (concat, head, tail) live here; generic iteration — including Iter.count (O(1) via the known_count protocol) — goes through Iter: Iter.map(xs, f) |> Iter.to_list() for the strict, List-returning form.

Import with import std/lists.

Types

type List<T>

Ordered, immutable sequence of elements.

fn next_item<T>(list: List<T>): Maybe<(T, List<T>)>

type function on List

Returns the next element and remaining list, or None if empty. Drives the Iter implementation for List.

Interactive Tests

assert List.next_item([1, 2, 3]) == Some((1, [2, 3]))
assert List.next_item([]) == None
fn concat<T>(a: List<T>, b: List<T>): List<T>

type function on List

Concatenates two lists, yielding all elements of a followed by all of b.

Interactive Tests

assert List.concat([1, 2], [3, 4]) == [1, 2, 3, 4]
fn head<T>(list: List<T>): Maybe<T>

type function on List

Returns the first element, or None if the list is empty. The cons-list idiom, paired with tail; the stream-vocabulary equivalent is the free function Iter.first.

Interactive Tests

assert List.head([1, 2, 3]) == Some(1)
assert List.head([]) == None
fn tail<T>(list: List<T>): Maybe<List<T>>

type function on List

Returns all elements except the first, or None if the list is empty.

Interactive Tests

assert List.tail([1, 2, 3]) == Some([2, 3])
assert List.tail([]) == None
fn each_while(list: List<T>, yield: (T) -> Bool): Bool

impl Iter.each_while

fn known_count(list: List<T>): Maybe<Int>

impl Iter.known_count

fn add(lhs: List<T>, rhs: List<T>): List<T>

impl Add.add

fn to_string(value: List<T>): String

impl Display.to_string

fn inspect(value: List<T>): String

impl Debug.inspect

fn equal?(a: List<T>, b: List<T>): Bool

impl Equatable.equal?

fn hash(list: List<T>): Int

impl Hashable.hash

fn compare(a: List<T>, b: List<T>): Ordering

impl Comparable.compare