Skip to main content

List

Import List for operations on [a]. Unless noted otherwise, functions preserve input order and return new lists without modifying their inputs.

Shape

listPrepend

listPrepend :: a -> [a] -> [a].

Adds a value to the front of a list in O(1).

listReverse

listReverse :: [a] -> [a].

Returns the values in reverse order in O(n).

listLength

listLength :: [a] -> Int.

Counts the values in O(n).

listIsEmpty

listIsEmpty :: [a] -> Bool.

Returns True only for []. This is O(1).

Safe access

listHead

listHead :: [a] -> Maybe(a).

Returns Just containing the first value, or Nothing for []. This is O(1).

listTail

listTail :: [a] -> Maybe([a]).

Returns every value after the head, or Nothing for []. This is O(1).

listLast

listLast :: [a] -> Maybe(a).

Returns the final value, or Nothing for [], in O(n).

listInit

listInit :: [a] -> Maybe([a]).

Returns every value except the last, or Nothing for [], in O(n).

listAt

listAt :: Int -> [a] -> Maybe(a).

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

listTake :: Int -> [a] -> [a].

Returns at most the first count values. Counts below zero clamp to zero. The cost is linear in the returned prefix.

listDrop

listDrop :: Int -> [a] -> [a].

Skips at most the first count values. Counts below zero clamp to zero. The cost is linear in the skipped prefix.

listSplitAt

listSplitAt :: Int -> [a] -> ([a], [a]).

Returns (listTake count values, listDrop count values). Negative counts yield an empty prefix and the original list as the suffix.

Combining

listAppend

listAppend :: [a] -> [a] -> [a].

Returns the left list followed by the right list. The cost is O(n) in the left list.

listConcat

listConcat :: [[a]] -> [a].

Flattens lists from left to right. The cost is linear in the fragment count and the total number of produced values.

listRepeat

listRepeat :: Int -> a -> [a].

Returns count copies of a value. Non-positive counts return []. The cost is linear in the output length.

listIntersperse

listIntersperse :: a -> [a] -> [a].

Places the separator between adjacent values, never before the first or after the last. This is linear in the output length.

listIntercalate

listIntercalate :: [a] -> [[a]] -> [a].

Inserts the separator list between adjacent fragments and concatenates the result. Work is linear in fragments plus produced values.

Transforming

listMap

listMap :: (a -> b) -> [a] -> [b].

Transforms every value from left to right and preserves order. This is O(n) plus callback work.

listFilter

listFilter :: (a -> Bool) -> [a] -> [a].

Keeps values whose predicate returns True, preserving their order. This is O(n) plus callback work.

listFilterMap

listFilterMap :: (a -> Maybe(b)) -> [a] -> [b].

Calls the transform for each value, keeping values inside Just and discarding Nothing. Output order matches input order.

listPartition

listPartition :: (a -> Bool) -> [a] -> ([a], [a]).

Returns matching values first and rejected values second. Both lists preserve input order. This is O(n) plus callback work.

Folding

listFoldLeft

listFoldLeft :: (b -> a -> b) -> b -> [a] -> b.

Combines values from left to right, starting with the supplied accumulator. This is O(n) plus callback work.

listFoldRight

listFoldRight :: (a -> b -> b) -> b -> [a] -> b.

Combines values from right to left, starting with the supplied terminal value. This is O(n) plus callback work.

listScanLeft

listScanLeft :: (b -> a -> b) -> b -> [a] -> [b].

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].

listAny

listAny :: (a -> Bool) -> [a] -> Bool.

Returns True at the first matching value and short-circuits. It returns False for [] and is O(n) worst case.

listAll

listAll :: (a -> Bool) -> [a] -> Bool.

Returns False at the first rejected value and short-circuits. It returns True for [] and is O(n) worst case.

listContains

listContains :: @{Eq(a)}: a -> [a] -> Bool.

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

listFind :: (a -> Bool) -> [a] -> Maybe(a).

Returns the first matching value as Just, or Nothing when no value matches. The search short-circuits and is O(n) worst case.

listFindIndex

listFindIndex :: (a -> Bool) -> [a] -> Maybe(Int).

Returns the zero-based index of the first match, or Nothing. The search short-circuits and is O(n) worst case.

Pair views

listZip

listZip :: [a] -> [b] -> [(a, b)].

Pairs corresponding values and stops when either input ends. Example: listZip [1, 2] ["a"] produces [(1, "a")].

listUnzip

listUnzip :: [(a, b)] -> ([a], [b]).

Separates pairs into left and right lists while preserving pair order. This is O(n).

listIndexed

listIndexed :: [a] -> [(Int, a)].

Pairs values with zero-based indices in input order. This is O(n).

Normalization

listDistinct

listDistinct :: @{Eq(a)}: [a] -> [a].

Removes repeated values while preserving the first occurrence of each value. This requires Eq(a) and is O(n²) worst case.

listGroup

listGroup :: @{Eq(a)}: [a] -> [[a]].

Groups adjacent equal values. Equal values separated by another value remain in different groups. This is O(n) plus equality work.

listGroupBy

listGroupBy :: (a -> a -> Bool) -> [a] -> [[a]].

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

listMinimum :: @{Ord(a)}: [a] -> Maybe(a).

Returns the least value, or Nothing for [], in O(n).

listMaximum

listMaximum :: @{Ord(a)}: [a] -> Maybe(a).

Returns the greatest value, or Nothing for [], in O(n).

listSort

listSort :: @{Ord(a)}: [a] -> [a].

Returns a stable ascending merge sort using Ord(a). The cost is O(n log n).

listSortBy

listSortBy :: (a -> a -> Ordering) -> [a] -> [a].

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.