List
Import List for operations on [a]. Unless noted otherwise, functions
preserve input order and return new lists without modifying their inputs.
Shape
listPrepend
Adds a value to the front of a list in O(1).
listReverse
Returns the values in reverse order in O(n).
listLength
Counts the values in O(n).
listIsEmpty
Returns True only for []. This is O(1).
Safe access
listHead
Returns Just containing the first value, or Nothing for []. This is
O(1).
listTail
Returns every value after the head, or Nothing for []. This is O(1).
listLast
Returns the final value, or Nothing for [], in O(n).
listInit
Returns every value except the last, or Nothing for [], in O(n).
listAt
Returns the zero-based value at the index. Negative and out-of-range indices
return Nothing. The cost is O(min(n, index)) for a non-negative index.
Slicing
listTake
Returns at most the first count values. Counts below zero clamp to zero. The
cost is linear in the returned prefix.
listDrop
Skips at most the first count values. Counts below zero clamp to zero. The
cost is linear in the skipped prefix.
listSplitAt
Returns (listTake count values, listDrop count values). Negative counts yield
an empty prefix and the original list as the suffix.
Combining
listAppend
Returns the left list followed by the right list. The cost is O(n) in the
left list.
listConcat
Flattens lists from left to right. The cost is linear in the fragment count and the total number of produced values.
listRepeat
Returns count copies of a value. Non-positive counts return []. The cost is
linear in the output length.
listIntersperse
Places the separator between adjacent values, never before the first or after the last. This is linear in the output length.
listIntercalate
Inserts the separator list between adjacent fragments and concatenates the result. Work is linear in fragments plus produced values.
Transforming
listMap
Transforms every value from left to right and preserves order. This is O(n)
plus callback work.
listFilter
Keeps values whose predicate returns True, preserving their order. This is
O(n) plus callback work.
listFilterMap
Calls the transform for each value, keeping values inside Just and discarding
Nothing. Output order matches input order.
listPartition
Returns matching values first and rejected values second. Both lists preserve
input order. This is O(n) plus callback work.
Folding
listFoldLeft
Combines values from left to right, starting with the supplied accumulator.
This is O(n) plus callback work.
listFoldRight
Combines values from right to left, starting with the supplied terminal value.
This is O(n) plus callback work.
listScanLeft
Returns the initial accumulator followed by every successive left-fold result, so the output has one more value than the input.
Example: listScanLeft (\(sum, value) -> sum + value) 0 [1, 2, 3] produces
[0, 1, 3, 6].
Search
listAny
Returns True at the first matching value and short-circuits. It returns
False for [] and is O(n) worst case.
listAll
Returns False at the first rejected value and short-circuits. It returns
True for [] and is O(n) worst case.
listContains
Tests equality against values from left to right and short-circuits at the
first match. This is O(n) worst case and requires Eq(a).
listFind
Returns the first matching value as Just, or Nothing when no value matches.
The search short-circuits and is O(n) worst case.
listFindIndex
Returns the zero-based index of the first match, or Nothing. The search
short-circuits and is O(n) worst case.
Pair views
listZip
Pairs corresponding values and stops when either input ends. Example:
listZip [1, 2] ["a"] produces [(1, "a")].
listUnzip
Separates pairs into left and right lists while preserving pair order. This is
O(n).
listIndexed
Pairs values with zero-based indices in input order. This is O(n).
Normalization
listDistinct
Removes repeated values while preserving the first occurrence of each value.
This requires Eq(a) and is O(n²) worst case.
listGroup
Groups adjacent equal values. Equal values separated by another value remain
in different groups. This is O(n) plus equality work.
listGroupBy
Groups runs according to adjacent comparisons. A run continues while the
predicate returns True for each value and the value immediately following it.
This is O(n) plus callback work.
Ordering
listMinimum
Returns the least value, or Nothing for [], in O(n).
listMaximum
Returns the greatest value, or Nothing for [], in O(n).
listSort
Returns a stable ascending merge sort using Ord(a). The cost is
O(n log n).
listSortBy
Returns a stable merge sort ordered by the comparator. LT places the left
value first; EQ preserves the input order of equal values. The cost is
O(n log n) plus comparator work.
Functions that may not find a value return Maybe.