Lists
Lists are zero-terminated, nested row 2-tuples. They are declared by prepending a ~ to what looks like row syntax, like this: ~[] (in the REPL we have to wrap this in parentheses):
NIL
(NIL)
> NILEvaluates to 0.
NIL == 0 ; the empty listCONS
(CONS x xs)
> x : a
> xs : List a
> List aConstructs a new list by adding an element to the front of an existing list.
CONS 1 NIL == [1 0] ; a list with one element
CONS b#a (CONS b#b NIL) == [b#a [b#a 0]] ; a list with two elements
CONS 1 (CONS 2 (CONS 3 NIL)) == [1 [2 [3 0]]] ; a list with three elementslistCase
(listCase xs d k)
> xs : List a
> d : a
> k : a
> aPattern matches on a list, providing cases for empty and non-empty lists.
listCase ~[1 69 420 1337] 9001 listIdx == 420listSing
(listSing x)
> x : a
> List aCreates a singleton list containing one element.
listSing 5 == [5 0]
listSing b#hello == [b#hello 0]
listSing [] == [[] 0]listMap
(listMap f xs)
> f : (a > b)
> xs : List a
> List bApplies a function to every element of a list.
listMap (mul 2) ~[1 2 3] == [2 [4 [6 0]]]
listMap isNat (CONS 3 (CONS b#a NIL)) == [1 [0 0]]
listMap id NIL == 0 ; NILlistForEach
(listForEach xs f)
> xs : List a
> f : (a > b)
> List bAlias for listMap. Applies a function to every element of a list.
listForEach (CONS 1 (CONS 2 (CONS 3 NIL))) (mul 2) == [2 [4 [6 0]]]
listForEach (~[3 [b#a 0]]) isNat == [1 [0 0]]
listForEach NIL id == NILlistHead
(listHead xs)
> xs : List a
> aReturns the first element of a list.
listHead (CONS 2 (CONS 3 NIL)) == (0 2)
listHead (CONS b#a NIL) == (0 b#a)
listHead NIL == 0listSafeHead
(listSafeHead d xs)
> d : a
> xs : List a
> aReturns the first element of a list, or a fallback value if the list is empty.
listSafeHead 0 (CONS 1 (CONS 2 NIL)) == 1
listSafeHead b#x (CONS b#a NIL) == b#a
listSafeHead b#default NIL == b#defaultlistUnsafeHead
(listUnsafeHead xs)
> xs : List a
> aReturns the first element of a list, otherwise 0. Unsafe if the list is empty.
listUnsafeHead (CONS 1 (CONS 2 NIL)) == 1
listUnsafeHead (CONS b#a NIL) == b#a
listUnsafeHead NIL == 0 ; NILlistUnsafeTail
(listUnsafeTail xs)
> xs : List a
> List aReturns the tail of a list (all elements except the first). Unsafe if the list is empty.
listUnsafeTail (CONS 1 (CONS 2 NIL)) == [2 0]
listUnsafeTail (CONS b#a NIL) == 0
listUnsafeTail NIL == 0 ; NILlistIdxCps
(listIdxCps i xs d k)
> i : Nat
> xs : List a
> d : a
> k : (a > b)
> aContinuation-passing style function to get the element at a given index in a list.
listIdxCps 1 (CONS b#a (CONS b#b NIL)) b#{not found} id == b#b
listIdxCps 0 (CONS 1 NIL) b#{not found} id == 1
listIdxCps 2 (CONS 1 (CONS 2 NIL)) b#{not found} id == b#{not found}listIdxOr
(listIdxOr d i xs)
> d : a
> i : Nat
> xs : List a
> aReturns the element at a given index in a list, or a fallback value if the index is out of bounds.
listIdxOr 0 1 (CONS b#a (CONS b#b NIL)) == b#b
listIdxOr b#z 99 (CONS b#a (CONS b#b NIL)) == b#z
listIdxOr b#default 0 NIL == b#defaultlistIdx
(listIdx i xs)
> i : Nat
> xs : List a
> aReturns the element at a given index in a list, or 0 if the index is out of bounds.
listIdx 1 (CONS b#a (CONS b#b NIL)) == b#b
listIdx 0 (CONS 1 NIL) == 1
listIdx 2 (CONS 1 (CONS 2 NIL)) == 0listLastOr
(listLastOr d xs)
> d : a
> xs : List a
> aReturns the last element of a list, or a fallback value if the list is empty.
listLastOr 0 (CONS 1 (CONS 2 NIL)) == 2
listLastOr b#z ~[b#a 0] == 0
listLastOr b#z ~[] == b#zlistUnsafeLast
(listUnsafeLast xs)
> xs : List a
> aReturns the last element of a list. Unsafe if the list is empty.
listUnsafeLast (CONS 1 (CONS 2 NIL)) == 2
listUnsafeLast (CONS b#a NIL) == b#a
listUnsafeLast NIL == 0 ; NILlistLast
(listLast xs)
> xs : List a
> aReturns the last element of a list.
listLast (CONS 1 (CONS 2 NIL)) == (0 2)
listLast ~[b#a] == (0 b#a)
listLast NIL == 0listFoldl
(listFoldl f z xs)
> f : (b > a > b)
> z : b
> xs : List a
> bLeft-associative fold of a list.
listFoldl add 0 ~[1 2 3] == 6
listFoldl barWeld b#{} ~[b#a b#b b#c] == b#abc
listFoldl (flip CONS) NIL ~[1 2 3] == [3 [2 [1 0]]]listFoldl1
(listFoldl1 f xs)
> f : (a > a > a)
> xs : List a
> aLeft-associative fold of a non-empty list, using the first element as the initial accumulator.
listFoldl1 add ~[2 3 4] == 9
listFoldl1 max (CONS 1 (CONS 5 (CONS 3 NIL))) == 5
listFoldl1 barWeld ~[b#a b#b b#c] == b#abclistFoldr
(listFoldr f z xs)
> f : (a > b > b)
> z : b
> xs : List a
> bRight-associative fold of a list.
listFoldr sub 0 (~[1 2 3]) == 1
listFoldr barWeld b#{} ~[b#a b#b b#c] == b#abc
listFoldr (flip CONS) NIL ~[1 2 3] == [[[0 3] 2] 1]listLen
(listLen xs)
> xs : List a
> NatComputes the length of a list.
listLen (CONS 1 (CONS 2 (CONS 3 NIL))) == 3
listLen (CONS b#a NIL) == 1
listLen NIL == 0listToRow
(listToRow xs)
> xs : List a
> Row aConverts a list to a row.
listToRow ~[1 2 3] == [1 2 3]
listToRow (CONS b#a (CONS b#b NIL)) == [b#a b#b]
listToRow NIL == []sizedListToRow
(sizedListToRow n xs)
> n : Nat
> xs : List a
> Row aConverts a list to a row of a specified size, padding with zeros if necessary.
sizedListToRow 3 ~[1 2] == [1 2 0]
sizedListToRow 2 (CONS 1 (CONS 2 (CONS 3 NIL))) == [1 2]
sizedListToRow 4 NIL == [0 0 0 0]sizedListToRowRev
(sizedListToRowRev n xs)
> n : Nat
> xs : List a
> Row aConverts a list to a row of a specified size in reverse order, padding with zeros if necessary.
sizedListToRowRev 3 (CONS 1 (CONS 2 NIL)) == [0 2 1]
sizedListToRowRev 2 ~[1 2 3] == [2 1]
sizedListToRowRev 4 NIL == [0 0 0 0]listToRowRev
(listToRowRev xs)
> xs : List a
> Row aConverts a list to a row in reverse order.
listToRowRev (CONS 1 (CONS 2 (CONS 3 NIL))) == [3 2 1]
listToRowRev (~[b#a b#b]) == [b#b b#a]
listToRowRev NIL == []listFromRow
(listFromRow xs)
> xs : Row a
> List aConverts a row to a list.
listFromRow [1 2 3] == [1 [2 [3 0]]]
listFromRow [b#a b#b] == [b#a [b#b 0]]
listFromRow (gen 4 id) == [0 [1 [2 [3 0]]]]listAnd
(listAnd xs)
> xs : List Bool
> BoolReturns TRUE if all elements in the list are TRUE, otherwise FALSE.
listAnd (CONS TRUE (CONS TRUE NIL)) == 1 ; TRUE
listAnd (CONS TRUE (CONS FALSE NIL)) == 0 ; FALSE
listAnd NIL == 1 ; TRUElistOr
(listOr xs)
> xs : List Bool
> BoolReturns TRUE if any element in the list is TRUE, otherwise FALSE.
listOr (CONS FALSE (CONS TRUE NIL)) == 1 ; TRUE
listOr ~[FALSE 0] == 0 ; FALSE
listOr NIL == 0 ; FALSElistSum
(listSum xs)
> xs : List Nat
> NatComputes the sum of all elements in a list of numbers.
listSum (CONS 1 (CONS 2 (CONS 3 NIL))) == 6
listSum (~[1 2 3]) == 6
listSum NIL == 0listAll
(listAll f xs)
> f : (a > Bool)
> xs : List a
> BoolReturns TRUE if all elements in the list satisfy the given predicate.
listAll even (CONS 2 (CONS 4 (CONS 6 NIL))) == 1 ; TRUE
listAll (gte 1) (~[1 2 3]) == 0 ; FALSE
listAll id NIL == 1 ; TRUElistAllEql
(listAllEql xs)
> xs : List a
> BoolReturns TRUE if all elements in the list are equal.
listAllEql (CONS 1 (CONS 1 (CONS 1 NIL))) == 1 ; TRUE
listAllEql (~[b#a b#a]) == 1 ; TRUE
listAllEql (CONS 1 (CONS 2 NIL)) == 0 ; FALSElistAny
(listAny f xs)
> f : (a > Bool)
> xs : List a
> BoolReturns TRUE if any element in the list satisfies the given predicate.
listAny odd (CONS 2 (CONS 3 (CONS 4 NIL))) == 1 ; TRUE
listAny (gte 0) (~[1 2 3]) == 0 ; FALSE
listAny id NIL == 0 ; FALSElistHas
(listHas e xs)
> e : a
> xs : List a
> BoolChecks if a list contains a specific element.
listHas 2 (CONS 1 (CONS 2 (CONS 3 NIL))) == 1 ; TRUE
listHas b#a (CONS b#b (CONS b#c NIL)) == 0 ; FALSE
listHas 1 NIL == 0 ; FALSElistEnumFrom
(listEnumFrom x)
> x : Nat
> List aCreates an infinite list of consecutive integers starting from a given number.
listTake 5 (listEnumFrom 1) == [1 [2 [3 [4 [5 0]]]]]
listHead (listEnumFrom 10) == (0 10) ; SOME
listIdx 1 (listEnumFrom 7) == 8listWeld
(listWeld xs ys)
> xs : List a
> ys : List a
> List aConcatenates two lists.
listWeld (CONS 1 (CONS 2 NIL)) (CONS 3 (CONS 4 NIL)) == [1 [2 [3 [4 0]]]]
listWeld ~[b#a] ~[b#b] == [b#a [b#b 0]]
listWeld NIL (CONS 1 NIL) == [1 0]listCat
(listCat xss)
> xss : List (List a)
> List aConcatenates a list of lists into a single list.
listCat ~[~[1 2] ~[3]] == [1 [2 [3 0]]]
listCat (CONS NIL (CONS NIL NIL)) == 0 ; NILlistCatMap
(listCatMap f xs)
> f : (a > List b)
> xs : List a
> List bApplies a function to all elements in a list and concatenates the results.
listCatMap (x & ~[x x]) ~[1 2] == [1 [1 [2 [2 0]]]]
listCatMap (x & ~[x]) ~[b#a b#b] == [b#a [b#b 0]]
listCatMap (x & ~[]) ~[1 2 3] == 0 ; NILlistTake
(listTake n xs)
> n : Nat
> xs : List a
> List aTakes the first n elements from a list.
listTake 2 (CONS 1 (CONS 2 (CONS 3 NIL))) == [1 [2 0]]
listTake 3 ~[1 2] == [1 [2 0]]
listTake 0 (CONS 1 NIL) == 0 ; NILlistDrop
(listDrop n xs)
> n : Nat
> xs : List a
> List aDrops the first n elements from a list.
listDrop 2 (CONS 1 (CONS 2 (CONS 3 NIL))) == [3 0]
listDrop 3 ~[1 2] == 0 ; NIL
listDrop 0 (CONS 1 NIL) == [1 0]listTakeWhile
(listTakeWhile f xs)
> f : (a > Bool)
> xs : List a
> List aTakes elements from the front of a list while they satisfy a predicate.
listTakeWhile (gte 2) (CONS 1 (CONS 2 (CONS 3 (CONS 4 NIL)))) == [1 [2 0]]
listTakeWhile even ~[2 4 5 6] == [2 [4 0]]
listTakeWhile (const TRUE) NIL == 0 ; NILlistDropWhile
(listDropWhile f xs)
> f : (a > Bool)
> xs : List a
> List aDrops elements from the front of a list while they satisfy a predicate.
listDropWhile (gte 2) (CONS 1 (CONS 2 (CONS 3 (CONS 4 NIL)))) == [3 [4 0]]
listDropWhile even ~[2 4 5 6] == [5 [6 0]]
listDropWhile (const TRUE) NIL == 0 ; NILlistZipWith
(listZipWith f xs ys)
> f : (a > a > b)
> xs : List a
> ys : List a
> List bCombines two lists element-wise using a given function.
listZipWith add (CONS 1 (CONS 2 NIL)) (CONS 3 (CONS 4 NIL)) == [4 [6 0]]
listZipWith v2 ~[1 2 3] ~[4 5] == [[1 4] [[2 5] 0]]
listZipWith mul NIL (CONS 1 NIL) == 0 ; NILlistZip
(listZip xs ys)
> xs : List a
> ys : List a
> List (a, a)Combines two lists into a list of pairs.
listZip (CONS 1 (CONS 2 NIL)) (CONS 3 (CONS 4 NIL)) == [[1 3] [[2 4] 0]]
listZip ~[1 2 3] ~[4 5] == [[1 4] [[2 5] 0]]
listZip NIL (CONS 1 NIL) == 0 ; NILlistFilter
(listFilter f xs)
> f : (a > Bool)
> xs : List a
> List aKeeps only the elements of a list that satisfy a predicate.
listFilter even (CONS 1 (CONS 2 (CONS 3 (CONS 4 NIL)))) == [2 [4 0]]
listFilter (lte 3) ~[1 2 3 4 5] == [3 [4 [5 0]]]
listFilter (const TRUE) NIL == 0 ; NILlistIsEmpty
(listIsEmpty xs)
> xs : List a
> BoolChecks if a list is empty.
listIsEmpty NIL == 1 ; TRUE
listIsEmpty (CONS 1 NIL) == 0 ; FALSE
listIsEmpty (CONS 1 (CONS 2 NIL)) == 0 ; FALSElistMinimumOn
(listMinimumOn f x xs)
> f : (a > Nat)
> x : a
> xs : List a
> aFinds the minimum element in a list based on a comparison function.
listMinimumOn len [456] ~[[5 2 9] [2 9] [1 9]] == [456]
listMinimumOn len [456] ~[[5 2 9] [] [1 9]] == []
listMinimumOn len [456] ~[[5 2 9] [2] [1 9]] == [456]listSortOn
(listSortOn f xs)
> f : (a > b)
> xs : List a
> List bSorts a list based on a key function.
listSortOn id (CONS 3 (CONS 1 (CONS 2 NIL))) == [1 [2 [3 0]]]
listSortOn (div 1) ~[1 2 3] == [3 [2 [1 0]]]
listSortOn len ~[[1 2] [3] [4 5 6]] == [[3] [[1 2] [[4 5 6] 0]]]listNub
(listNub xs)
> xs : List a
> List aRemoves duplicate elements from a list, keeping only the first occurrence.
listNub (CONS 1 (CONS 2 (CONS 1 (CONS 3 NIL)))) == [1 [2 [3 0]]]
listNub ~[3 2 1 2 1] == [3 [2 [1 0]]]
listNub NIL == 0 ; NILlistIterate
(listIterate f x)
> f : (a > a)
> x : a
> List aGenerates an infinite list by repeatedly applying a function to an initial value.
listTake 5 (listIterate inc 0) == [0 [1 [2 [3 [4 0]]]]]
listTake 3 (listIterate (mul 2) 1) == [1 [2 [4 0]]]
listTake 0 (listIterate id 1) == 0 ; NILlistGen
(listGen n f)
> n : Nat
> f : (a > a)
> List aGenerates a list of n elements using a given function.
listGen 3 id == [0 [1 [2 0]]]
listGen 4 (const b#a) == [b#a [b#a [b#a [b#a 0]]]]
listGen 0 (const 1) == 0 ; NILlistRep
(listRep e n)
> e : a
> n : Nat
> List aGenerates a list of n copies of a given element.
listRep 1 3 == [1 [1 [1 0]]]
listRep b#a 2 == [b#a [b#a 0]]
listRep 0 0 == 0 ; NILlistFindIndex
(listFindIndex f xs d m)
> f : (a > b)
> xs : List a
> d : b
> k : (Nat > b)
> aFinds the index of the first element in a list that satisfies a predicate. If there is none, the third argument is returned. If there is one, then its index is passed as an argument to the fourth argument.
listFindIndex eql-10 ~[1 2 3] b#NONE _&(b#SOME) == b#NONE
listFindIndex (eql b#a) ~[b#b b#a b#c] NONE SOME == (0 1) ; SOME
listFindIndex (const FALSE) ~[1 2 3] NONE SOME == 0 ; NONElistElemIndex
(listElemIndex e xs d k)
> e : a
> xs : List a
> d : b
> k : (a > b)
> bFinds the index of the first occurrence of an element in a list.
listElemIndex 2 (CONS 1 (CONS 2 (CONS 3 NIL))) NONE SOME == (0 1)
listElemIndex b#a ~[b#b b#c b#c b#a] NONE SOME == (0 3)
listElemIndex 4 ~[1 2 3] NONE SOME == 0listIsPrefixOf
(listIsPrefixOf xs ys)
> xs : List a
> ys : List a
> BoolChecks if one list is a prefix of another list.
listIsPrefixOf (CONS 1 (CONS 2 NIL)) (CONS 1 (CONS 2 (CONS 3 NIL))) == 1 ; TRUE
listIsPrefixOf ~[1 2] ~[1 2 3] == 1 ; TRUE
listIsPrefixOf ~[1 2] ~[2 1] == 0 ; FALSElistSearch
Searches for all occurrences of a list satisfying a predicate and returns their indices and the remaining lists.
TODOlistSubstringSearch
Searches for all occurrences of a substring in a list and returns their indices and the remaining lists.
TODOlistIndexed
(listIndexed xs)
> xs : List a
> List (Row a)Pairs each element in a list with its index.
listIndexed (CONS 1 (CONS 2 (CONS 3 NIL))) == [[0 1] [[1 2] [[2 3] 0]]]
listIndexed ~[b#a b#b] == [[0 b#a] [[1 b#b] 0]]
listIndexed NIL == 0 ; NILlistIntersperse
(listIntersperse e xs)
> e : a
> xs : List a
> List aIntersperses an element between every element of a list.
listIntersperse 0 (CONS 1 (CONS 2 (CONS 3 NIL))) == [1 [0 [2 [0 [3 0]]]]]
listIntersperse b#a ~[b#b] == [b#b 0]
listIntersperse 0 NIL == 0 ; NILlistRev
(listRev xs)
> xs : List a
> List aReverses a list.
listRev (CONS 1 (CONS 2 (CONS 3 NIL))) == [3 [2 [1 0]]]
listRev ~[b#a b#b] == [b#b [b#a 0]]
listRev NIL == 0 ; NILlistSnoc
(listSnoc xs e)
> xs : List a
> e : a
> List aAdds an element to the end of a list.
listSnoc (CONS 1 (CONS 2 NIL)) 3 == [1 [2 [3 0]]]
listSnoc ~[b#a] b#b == [b#a [b#b 0]]
listSnoc NIL 1 == [1 0]listProd
(listProd xs ys)
> xs : List a
> ys : List b
> List (Row a b)Computes the Cartesian product of two lists.
listProd (CONS 1 (CONS 2 NIL)) (CONS 3 (CONS 4 NIL)) == [[1 3] [[1 4] [[2 3] [[2 4] 0]]]]
listProd ~[1 2] ~[b#a b#b] == [[1 b#a] [[1 b#b] [[2 b#a] [[2 b#b] 0]]]]
listProd NIL (CONS 1 NIL) == 0 ; NILLast updated