Skip to content

std/list

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 the iter module: Iter.map(xs, f) |> Iter.to_list() for the strict, List-returning form.

Import with import std/list.

Types

type List<'T>

Ordered, immutable sequence of elements.

fn list_next(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. (Named list_next rather than next so it doesn’t collide with the Iterable.next function on List.)

Interactive Tests

assert List.list_next([1, 2, 3]) == Some((1, [2, 3]))
assert List.list_next([]) == None
fn concat(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(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(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

impl Iterable

fn next(list: List<'T>): Maybe<('T, List<'T>)>

impl Iterable.next

impl Iterable

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

impl Iterable.known_count

impl Add

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

impl Add.add

impl Display

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

impl Display.to_string

impl Debug

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

impl Debug.inspect

impl Equatable

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

impl Equatable.equal?

impl Hashable

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

impl Hashable.hash

impl Comparable

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

impl Comparable.compare