Purpose: Handling lists.


[] macro

Given a whitespace-seperated enumeration of elements, builds a List of them in order.

Example:

[1 2 3 4 100]

nil

Alternative "name": []

Represents the empty list.

Example:

nil

nil? \l

Result is whether l is the empty list (and any list at all).

Example:

if (nil? nil) 'yes else 'no => yes

(:) \h \t

Result is a list with head h and tail t.

Example:

if (list? (5:6:7:8:nil)) 'yes else 'no => yes
head (5:6:7:8:nil) => 5
tail (5:6:7:8:nil) => (6:7:8:nil)

cons? \l

Result is whether l is something constructed by (:) .

Example:

if (cons? (5:6:7:8:nil)) 'yes else 'no => yes
if (cons? nil) 'yes 'no => no
if (cons? 42) 'yes 'no => no

list?

Result is whether l is a list, i.e. something constructed by (:) or nil .

Example:

if (list? (5:6:7:8:nil)) 'yes else 'no => yes
if (list? nil) 'yes else 'no => yes
if (list? 42) 'yes else 'no => no

head \l

Result is the head of l.

Example:

head (5:6:7:8:nil) => 5
tail (5:6:7:8:nil) => (6:7:8:nil)

tail \l

Result is the tail of l.

Example:

head (5:6:7:8:nil) => 5
tail (5:6:7:8:nil) => (6:7:8:nil)
tail (8:nil) => nil

take \count \l

Result is a list with the first count elements of l.

Example:

take 2 (5:6:7:8:nil) => (5:6:nil)
take (-2) (5:6:7:8:nil) => nil

drop \count \l

Result is a list with all elements of l except for the first count elements.

Example:

drop 2 (5:6:7:8:nil) => (7:8:nil)
drop (-2) (5:6:7:8:nil) => (5:6:7:8:nil)

length \l

Result is the length of the list l. This is quite slow (O(n)).

Example:

drop 2 (5:6:7:8:nil) => (7:8:nil)
drop (-2) (5:6:7:8:nil) => (5:6:7:8:nil)

range \beginning \frontier \step

Result is a list of numbers.

The list starts with beginning (inclusive), listing all the numbers until frontier, spaced step apart.

Example:

range 0 10 1 => [0 1 2 3 4 5 6 7 8 9]
range 5 10 1 => [5 6 7 8 9]
range 5 5 1 => []
range 5 3 1 => []
range 0 10 2 => [0 2 4 6 8]

merge \l1 \l2

Given two sorted lists, the result is a list that contains elements of both of the list in result sorted order (using <=), preferring the first list.

Example:

merge [1 3 4 7 8 9] [2 5 6 10 11 12] => [1 2 3 4 5 6 7 8 9 10 11 12]

mergeBy \lessEqualComparer \l1 \l2

Given two sorted lists, the result is a list that contains elements of both of the list in result sorted order (using lessEqualComparer), preferring the first list.

Example:

mergeBy (<=) [1 3 4 7 8 9] [2 5 6 10 11 12] => [1 2 3 4 5 6 7 8 9 10 11 12]

mergeSort \list

Given two sorted lists, the result is a sorted list that contains elements of list in result sorted order (using <=).

Example:

mergeSort [5 4 3 2 1] => [1 2 3 4 5]

mergeSortBy \lessEqualComparator \list

Given two sorted lists, the result is a sorted list that contains elements of list in result sorted order (using lessEqualComparator).

Example:

mergeSortBy (<=) [5 4 3 2 1] => [1 2 3 4 5]

(Higher-Order Data Processing Functions in module "List")


map \mapper \list

Result is a list where each element is mapper applied to the element of the original list, in order.

Example:

define square \x x*x
map square [1 2 3] => [1 4 9]

filter \keep? \list

Result is a list of elements of list where keep? element is true.

Example:

filter (2<) [1 2 3 4] => [3 4]

zipBy \f \list1 \list2

Result is a list of (f (one element of list1) (one element of list2), until at least one of the lists runs out of elements.

Example:

zipBy (+) [1 2 3] [4 5] 0> [5 7]

foldr \f \v \list

Associates list elements to the right, two by two, mediated by f, until running out of elements. For the missing element, substitutes v.

Example:

foldr (+) 0 [1 2 3] => (1+(2+(3+0))) => 6
foldr (-) 0 [1 2 3] => (1-(2-(3-0))) => 2

foldl \f \v \list

Associates list elements to the left, two by two, mediated by f, until running out of elements. For the missing element, substitutes v.

Example:

foldl (+) 0 [1 2 3] => (((0+1)+2)+3) => 6
foldl (-) 0 [1 2 3] => (((0-1)-2)-3)) => (-6)

allTrue? \f \list

Whether all of the elements of the list list satisfy the predicate f.


anyTrue? \f \list

Whether any of the elements of the list list satisfy the predicate f.