Contents

A list is a homogeneous, ordered sequence of values with type List[T]. Lists are managed values, support indexing and iteration, and grow through concatenation. They are the language's default collection and the main subject of the functional-imperative style described later in the guide.

Creating a List#

A non-empty list is written with square brackets and comma-separated elements. The element type is the common type of the elements.

const numbers = [1, 2, 3]
const names = ["Ada", "Grace"]
const nested = [[1, 2], [3, 4]]

An empty list has no element type of its own. It needs a contextual type, which a declaration annotation or a function return type supplies.

var values List[i32] = []
const empty_names List[str] = []

fn none() List[i32] {
    return []
}

There is no List::new, no List[T]() constructor, and no push method. A list is created from a literal and grown with +=. The standard library follows the same rule.

fn map[T, U](items List[T], transform fn(T) U) List[U] {
    var result List[U] = []
    for item in items {
        result += [transform(item)]
    }
    return result
}

Element Type Inference#

Numeric elements unify to a common numeric type. An i32 and an i64 in the same literal produce List[i64]. Mixed signed and unsigned elements follow the usual integer unification and should be avoided when the result is not obvious.

const integers = [1, 2, 3]                 // List[i32]
const wide = [1, 2_000_000_000]            // List[i64]
const reals = [1.0, 2, 3]                  // List[f64]

A list of records, sums, or other managed values stores references to those values. A list of scalars stores the scalar values directly. Extern records are not allowed inside lists.

Length and Indexing#

The len property returns the number of elements as i32. Indexing uses a zero-based integer and returns the element type. Bounds are checked at runtime.

const values = [10, 20, 30]
print(values.len)       // 3
print(values[0])        // 10
print(values[2])        // 30

Indexing a list is an lvalue when the binding is mutable, so an element can be assigned. Assigning through a const binding is rejected.

var values = [10, 20, 30]
values[1] = 99
print(values[1])        // 99

The built-in get returns an Option instead of failing. It is available as a free function and as a method call.

const values = [10, 20, 30]
if values.get(5) is Some(value) {
    print(value)
} else {
    print("out of range")
}

Concatenation and Append#

The + operator concatenates two lists with the same element type. The += operator appends to a var binding and reassigns it to the result. A single-element append is the common idiom for building a list in a loop.

const first = [1, 2]
const second = [3, 4]
const all = first + second

var built = [0]
for i in 1..4 {
    built += [i]
}

Appending another list is also allowed. The result contains the elements of both operands. A self-append such as values += values is valid and duplicates the current contents.

var values = [1, 2, 3]
values += values
print(values.len)       // 6

+ produces a list value. += rebinds the variable to the produced list. Element assignment mutates the existing list in place. The distinction matters when another binding refers to the same list.

var left = [1, 2]
var right = left
right[0] = 9
print(left[0])          // 9

right = [7, 8]
print(left[0])          // 9: only the binding changed

Iteration#

A for item in list loop visits the elements in order. The loop variable is a copy for scalar elements and a reference for managed elements. The loop captures the list length when it starts, so appending during iteration does not extend the loop. Avoid relying on that detail; build a new list when the iteration produces a different sequence.

fn sum(values List[i32]) i32 {
    var total = 0
    for value in values {
        total += value
    }
    return total
}

A range loop is the usual way to iterate by index. Use it when the algorithm needs neighbouring elements or the index itself.

fn adjacent_pairs(values List[i32]) List[i32] {
    var out List[i32] = []
    for i in 0..values.len - 1 {
        out += [values[i] + values[i + 1]]
    }
    return out
}

Method Syntax and Pipelines#

A free function whose first parameter is a list can be called with method syntax. This is how the standard sequence helpers are designed, and it allows a pipeline to read left to right.

const result = [1, 2, 3, 4, 5]
    .map(fn(x) => x * 2)
    .filter(fn(x) => x > 4)

The pipeline is ordinary function application. values.map(f) means map(values, f), and values.filter(p) means filter(values, p). A user-defined function with a different meaning can shadow a standard helper in the current module, so name collisions should be resolved explicitly.

List Operations at a Glance#

Operation Syntax Result
Length xs.len i32
Index xs[i] T
Checked index get(xs, i) or xs.get(i) Option[T]
Element assignment xs[i] = value Updates the list
Concatenation xs + ys List[T]
Append and rebind xs += [value] List[T]
Element iteration for x in xs Visits each T
Index iteration for i in 0..xs.len Visits each index

Lists and Contracts#

A postcondition may describe a list's length, its ordering, or its relationship to the input. Such a postcondition is often the most valuable documentation a function can carry.

pure fn sorted(values List[i32]) bool {
    for i in 0..values.len - 1 {
        if values[i] > values[i + 1] {
            return false
        }
    }
    return true
}

fn sort(values List[i32]) List[i32]
    : post r => sorted(r)
    : post r => r.len == values.len
{
    // implementation
    return values
}

The loop in sorted is safe for an empty list because the range 0..-1 performs no iterations. When a postcondition calls a helper, the helper must be pure so that the compiler can use it during compile-time evaluation.

Performance and Aliasing#

Lists are heap-allocated and grow with a capacity that is at least the current length. Appending to a list in a loop may reallocate, but the language does not expose the capacity. Build a list by appending when the size is not known in advance, and reserve an explicit literal when the contents are fixed. Avoid sharing a mutable list between components unless mutation is part of the component's documented interface; returning a new list is usually clearer and safer.