x-lang

← Index

x/type/list

List: the list/sequence API, homed on the List class.

The list/sequence operations as static methods; core/list.x keeps the low-level layer (fold/map/filter globals + %-helpers) it is built on.

Element access is (List ref n lst) – the adjudicated name; list-ref/list-tail are Scheme-compat wrappers.

Class List

(List of . args)

Variadic literal: the arguments as a list – (List of …) is (list …), homed on the class for the constructor-verb symmetry.

Parameters:

Returns: LIST — List of the arguments

Examples:

(List of 1 2 3) => (1 2 3)

(List from-seq x)

Build a list from any iterable – the from-X conversion verb (Gen from-seq is its lazy twin; the boot layer normalizes through its private %as-list plumbing). Lists and nil pass through unchanged; anything else goes through (Iter new), which raises type on a value it cannot iterate.

Parameters:

Returns: LIST — The input as a proper list

(List iter lst)

An iterator over the list’s elements.

Parameters:

Returns: ITER — Iterator

(List fold f init lst)

Fold a function over a list from the left.

Block form: (List fold (acc x) body … init lst) – or (acc i x) with the 0-based index ahead of the element.

Parameters:

Returns: ANY — Final accumulated value

Examples:

(List fold + 0 '(1 2 3)) => 6

(List reduce f lst)

Fold without an initial value; uses the first element.

Block form: (List reduce (a b) body … lst).

Parameters:

(List scan f init lst)

Like fold, but returns a list of all intermediate values.

Block form: (List scan (acc x) body … init lst) – or (acc i x) with the 0-based index ahead of the element.

Parameters:

(List fold-right f init lst)

Fold from the right: elements combine last-to-first, callback (f acc element) like fold.

Block form: (List fold-right (acc x) body … init lst) – or (acc i x) with the 0-based index ahead of the element.

Parameters:

Returns: ANY — Final accumulated value

Examples:

(List fold-right (fn (_ acc x) (pair x acc)) () (list 1 2 3)) => (1 2 3)

(List length lst)

Return the number of elements.

Parameters:

(List ref n lst)

Return the element at index n (zero-based; negative counts from the end; coerced to INT); errors when n is unconvertible or out of range.

Parameters:

(List last lst)

Return the last element of a list.

Parameters:

(List init lst)

Return all elements except the last.

Parameters:

(List append . args)

Concatenate zero or more lists.

(List prepend x lst)

Add an element to the front of a list.

Parameters:

(List reverse lst)

Reverse a list.

Parameters:

(List flatten lst)

Recursively flatten nested lists into a single list.

Parameters:

(List map f . lsts)

Apply a function to each element. Supports multiple lists.

Block form: (List map (x) body … lsts) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: LIST — New list

(List filter pred lst)

Return elements that satisfy a predicate.

Block form: (List filter (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: LIST — Filtered list

(List for-each f . lsts)

Apply a function to each element for side effects.

Block form: (List for-each (x) body … lsts) – or (i x) for the 0-based index, then the element.

Parameters:

(List flat-map f lst)

Map then flatten one level.

Block form: (List flat-map (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List any? pred lst)

Return #t if any element satisfies the predicate.

Block form: (List any? (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List all? pred lst)

Return #t if all elements satisfy the predicate.

Block form: (List all? (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List none? pred lst)

Return #t if no element satisfies the predicate.

Block form: (List none? (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List empty? lst)

Return #t if the list is empty.

Parameters:

(List reject pred lst)

Return elements that do NOT satisfy a predicate.

Block form: (List reject (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: LIST — Filtered list

(List min lst)

The smallest element (by <); errors on an empty list.

Parameters:

Returns: ANY — Smallest element

Examples:

(List min (list 3 1 2)) => 1

(List max lst)

The largest element (by <); errors on an empty list.

Parameters:

Returns: ANY — Largest element

Examples:

(List max (list 3 1 2)) => 3

(List sum lst)

Sum all elements of a list.

Parameters:

Returns: INT — Sum

(List product lst)

Multiply all elements of a list.

Parameters:

Returns: INT — Product

(List find pred lst)

Return the first element satisfying a predicate, or nil.

Block form: (List find (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List find-index pred lst)

Return the index of the first element satisfying a predicate.

Block form: (List find-index (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: ANY — Zero-based index, or nil if not found

(List index-of x lst)

Return the index of the first occurrence of a value.

Parameters:

Returns: ANY — Zero-based index, or nil if not found

(List includes? x lst)

Test if a list contains a value.

Parameters:

Returns: BOOL — t if found

(List count-if pred lst)

Count elements satisfying a predicate.

Block form: (List count-if (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: INT — Count of matching elements

(List take n lst)

Take the first n elements of a list (n coerced to INT).

Parameters:

(List chunk n lst)

Split a list into successive n-element sublists; the last may be shorter.

Parameters:

Returns: LIST — List of chunks

Examples:

(List chunk 2 (list 1 2 3 4 5)) => ((1 2) (3 4) (5))

(List drop n lst)

Drop the first n elements of a list (n coerced to INT).

Parameters:

(List take-while pred lst)

Take elements from the front while predicate holds.

Block form: (List take-while (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List drop-while pred lst)

Drop elements from the front while predicate holds.

Block form: (List drop-while (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List split-at n lst)

Split a list at position n.

Parameters:

Returns: LIST — Pair of (taken dropped)

(List slice start end lst)

Extract a slice from start to end – the slice convention: (start, end-exclusive).

Parameters:

(List sub start n lst)

Extract n elements from start – the sub convention: (start, length); the counted twin of slice.

Parameters:

(List range start end)

Generate a list of integers from start to end (both coerced to INT).

Parameters:

Returns: LIST — List of integers

Examples:

(List range 0 5) => (0 1 2 3 4)

(List repeat n x)

Create a list of n copies of a value (n coerced to INT); count first, matching (Str8 repeat n s).

Parameters:

Returns: LIST — List of repeated values

(List times n f)

Apply a function to each index 0..n-1, collecting results (n coerced to INT). Count first, per the constructor-count rule (was (f n) before the V6 adjudication).

Block form: (List times n (x) body …) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: LIST — List of results

(List unfold pred f g seed)

Build a list by repeatedly applying step and value functions to a seed.

Parameters:

Returns: LIST — Generated list

(List iterate f n x)

Generate n values by repeatedly applying f (n coerced to INT).

Block form: (List iterate (x) body … n x) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: LIST — List of iterated values

(List zip a b)

Pair up corresponding elements from two lists as assocs – the result is an alist, so it feeds Dict from-alist and the Assoc API directly.

Parameters:

Returns: LIST — Alist of (a . b) assocs

Examples:

(List zip (list 1 2) (list 7 8)) => ((1 . 7) (2 . 8))

(List zip-with f a b)

Combine corresponding elements from two lists using a function.

Block form: (List zip-with (a b) body … a b).

Parameters:

Returns: LIST — Combined list

(List unzip alist)

Invert zip: an alist of (a . b) assocs becomes a list of two lists.

Parameters:

Returns: LIST — (firsts seconds)

Examples:

(List unzip (List zip (list 1 2) (list "a" "b"))) => ((1 2) ("a" "b"))

(List interleave a b)

Alternate elements from two lists, stopping at the shorter.

Parameters:

Returns: LIST — (a1 b1 a2 b2 …)

Examples:

(List interleave (list 1 3) (list 2 4)) => (1 2 3 4)

(List partition pred lst)

Split a list into elements that match and don’t match a predicate.

Block form: (List partition (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List group-by f lst)

Group list elements by a key function.

Block form: (List group-by (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: LIST — Alist of (key . elements), keys in first-seen order

(List sort cmp lst)

Stable merge sort using a comparison function: equal-key elements keep their input order.

Block form: (List sort (a b) body … lst).

Parameters:

(List sort-by f lst)

Sort by a key function (ascending).

Block form: (List sort-by (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List distinct lst)

Remove ALL duplicates (equal?), keeping each element’s first occurrence – unlike uniq, no sorting needed.

O(n^2) via equal?, so it works for every element type; hashable elements (symbols/strings/ints/chars) can dedupe O(n) through x/type/set instead.

Parameters:

Returns: LIST — lst without later duplicates

Examples:

(List distinct (list 1 2 1 3 2)) => (1 2 3)

(List uniq lst)

Remove consecutive duplicates from a sorted list.

Parameters:

(List uniq-by f lst)

Remove consecutive duplicates by key function.

Block form: (List uniq-by (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List intersperse sep lst)

Insert a separator between each element.

Parameters:

(List transpose lsts)

Transpose rows and columns of a list of lists.

Parameters:

Returns: LIST — Transposed list of lists

(List update n val lst)

Replace the element at index n (coerced to INT).

Parameters:

(List insert n val lst)

Insert a value at index n (coerced to INT; clamped: n<=0 prepends, n>=length appends).

Parameters:

(List remove start n lst)

Remove n elements starting at index (both coerced to INT).

Parameters:

(List adjust n f lst)

Apply a function to the element at index n (coerced to INT).

Block form: (List adjust n (x) body … lst) – or (i x) for the 0-based index, then the element.

Parameters:

(List list? x)

Test if a value is a proper list.

Parameters:

Returns: BOOL — t if proper list

(List second x)

Return the second element of a list.

Parameters:

Returns: ANY — The second element

(List third x)

Return the third element of a list.

Parameters:

Returns: ANY — The third element