This page is under active development. It may contain bugs or incomplete descriptions.
TODO: Update with function and type signatures.
This module defines operations on rows, which are arrays. Since PLAN is untyped, these are also used as the building blocks for tuples, records, and datatypes.
They are defined with []
:
Copy arr=[10 64 42]
arr
[10 64 42]
null
Copy (null xs)
> xs : Row a
> Bool
Checks if a row is empty.
null
returns TRUE
if the given row is empty, and FALSE
otherwise.
Copy null [] == 1
null [1 2 3] == 0
null [0] == 0
arity
Copy (arity x)
> x : a
> Nat
Returns the arity of a function or row.
arity
returns the number of arguments a function takes, or the number of elements in a row plus one.
Copy arity add == 2
arity [1 2 3] == 4
arity [] == 1
len
Copy (len xs)
> xs : Row a
> Nat
Returns the length of a row.
len
counts the number of elements in a given row.
Copy len [1 2 3] == 3
len [] == 0
len [9] == 1
idx
Copy (idx i xs)
> i : Nat
> xs : Row a
> a
Retrieves an element from a row at a specified index.
idx
returns the element at the given index in the row. Indexing starts at 0.
Copy idx 1 [10 20 30] == 20
idx 0 [5 6 7] == 5
idx 2 [1] == 0 (out of bounds)
last
Copy (last xs)
> xs : Row a
> a
Retrieves an element from the end of a row.
Copy last [10 20 30] == 30
last [5 6 7] == 7
last [] 2 == 0
get
Copy (get xs i)
> xs : Row a
> i : Nat
> a
Retrieves an element from a row at a specified index.
get
is an alias for idx
. It returns the element at the given index in the row.
Copy get [10 20 30] 1 == 20
get [5 6 7] 0 == 5
get [1] 2 == 0 (out of bounds)
mut
Copy (mut i v xs)
> i : Nat
> v : a
> xs : Row a
> Row a
Modifies an element in a row at a specified index.
mut
returns a new row with the element at the given index replaced by the provided value.
Copy mut 1 99 [10 20 30] == [10 99 30]
mut 0 5 [1 2 3] == [5 2 3]
mut 3 4 [1 2] == [1 2 0 4]
put
Copy (put xs i v)
> xs : Row a
> i : Nat
> v : a
> Row a
Modifies an element in a row at a specified index.
put
is an alias for mut
. It returns a new row with the element at the given index replaced by the provided value.
Copy put [10 20 30] 1 99 == [10 99 30]
put [1 2 3] 0 5 == [5 2 3]
put [1 2] 3 4 == [1 2 0 4]
switch
Copy (switch i d xs)
> i : Nat
> d : b
> xs : Row a
> a
Selects a value from a row based on an index, with a fallback value.
switch
returns the element at the given index if it exists, otherwise it returns the fallback value.
Copy switch 1 0 [10 20 30] == 20
switch 3 0 [10 20 30] == 0 (fallback)
switch 0 99 [] == 99 (fallback)
c0, c1, c2, c3, c4, c5, c6, c7, c8, c9
Creates a row constructor of the specified arity.
These functions create row constructors of arity 0 to 9 respectively.
Copy c3 1 2 3 == [3 2 1]
c0 == []
c2 b#a b#b == [b#b b#a]
v0, v1, v2, v3, v4, v5, v6, v7, v8, v9
Creates a row of the specified arity.
These functions create rows of arity 0 to 9 respectively, with the arguments in reverse order.
Copy v3 1 2 3 == [1 2 3]
v0 == []
v2 b#a b#b == [b#a b#b]
isRow
Copy (isRow x)
> x : a
> Bool
Checks if a value is a row.
isRow
returns TRUE
if the given value is a row, and FALSE
otherwise.
Copy isRow [1 2 3] == 1
isRow [] == 1
isRow 3 == 0
foldr
Copy (foldr f xs)
> f : (a > b)
> xs : Row a
> b
Folds a row from right to left.
foldr applies a binary function to a starting value (from the right) and all elements of the row, going from right to left.
Copy foldr sub 0 [1 2 3] == 1
foldr v2 [] [1 2 3] == [1 [2 [3 []]]]
foldr add 0 [] == 0
foldl
Copy (foldl f xs)
> f : (a > b)
> xs : Row a
> b
Folds a row from left to right.
foldl applies a binary function to a starting value (from the left) and all elements of the row, going from left to right.
Copy foldl sub 0 [1 2 3] == 0
foldl v2 [] [1 2 3] == [[[[] 1] 2] 3]
foldl add 0 [1] == 1
foldr1
Copy (foldr1 f xs)
> f : (a > b)
> xs : Row a
> b
Folds a non-empty row from right to left using the last element as the initial accumulator.
Copy foldr1 sub [1 2 3 10] == 0
foldr1 strWeld [%a %b %c] == %abc
foldr1 max [1 5 3 2] == 5
unfoldr
Copy (unfoldr f s)
> f : (a > b)
> s : a
> a
Builds a row from a seed value using a function that returns either a value and a new seed, or nothing.
Copy unfoldr n&(if (gth n 0) (SOME [n dec-n]) NONE) 5 == [5 4 3 2 1]
unfoldr n&(if (lth n 3) (SOME [n inc-n]) NONE) 0 == [0 1 2]
unfoldr _&NONE 0 == []
strictRow
Copy (strictRow x)
> x : a
> Row a
Forces evaluation of all elements in a row.
Copy strictRow [1 2 3] == [1 2 3] ; ensures all elements are evaluated
strictRow (map (mul 2) [1 2 3]) == [2 4 6] ; forces computation
strictRow [] == []
weld
Copy (weld xs ys)
> xs : Row a
> ys : Row a
> Row a
Concatenates two rows.
weld
combines two rows into a single row, with the elements of the first row followed by the elements of the second row.
Copy weld [1 2] [3 4] == [1 2 3 4]
weld [] [5 6] == [5 6]
weld [1 2] [] == [1 2]
gen
Copy (gen l f)
> l : Nat
> f : (Nat > a)
> Row a
Generates a row based on a function and a length.
gen
creates a row of the specified length, where each element is the result of applying the given function to its index.
Copy gen 3 (add 2) == [2 3 4]
gen 4 id == [0 1 2 3]
gen 0 | mul 2 == []
fst
Copy (fst xs)
> xs : Row a
> a
Returns the first element of a row.
fst
retrieves the leftmost element of a given row.
Copy fst [1 2 3] == 1
fst [9] == 9
fst [] == 0
snd
Copy (snd xs)
> xs : Row a
> a
Returns the second element of a row.
snd
retrieves the second element of a given row. If the row has fewer than two elements, it returns 0.
Copy snd [1 2 3] == 2
snd [9] == 0
snd [] == 0
thr
Copy (thr xs)
> xs : Row a
> a
Returns the third element of a row.
thr
retrieves the third element of a given row. If the row has fewer than three elements, it returns 0.
Copy thr [1 2 3 4] == 3
thr [1 2] == 0
thr [] == 0
map
Copy (map f xs)
> f : (a > b)
> xs : Row a
> Row b
Applies a function to each element of a row.
map
creates a new row by applying the given function to each element of the input row.
Copy map (mul 2) [1 2 3] == [2 4 6]
map fst [[1 2] [3 4] [5 6]] == [1 3 5]
map (add 1) [] == []
foreach
Alias for map
with arguments reversed.
foreach
is an alias for map
. It applies a function to each element of a row.
Copy foreach [1 2 3] (mul 2) == [2 4 6]
foreach [[1 2] [3 4] [5 6]] fst == [1 3 5]
foreach [] (add 1) == []
rev
Copy (rev xs)
> xs : Row a
> Row a
Reverses a row.
rev
creates a new row with the elements of the input row in reverse order.
Copy rev [1 2 3] == [3 2 1]
rev [9] == [9]
rev [] == []
curry
Copy (curry f x y)
> f : (Row a > b)
> x : a
> y : a
> a
Converts a function that takes a row to a curried function.
curry takes a function that expects a row as its argument and returns a function that takes two arguments separately.
Copy See the function definition in sire/sire_05_row.sire.
uncurry
Copy (uncurry f xs)
> f : (a > a)
> xs : Row a
> f (Row a)
Converts a curried function to a function that takes a row.
uncurry takes a function that expects two separate arguments and returns a function that takes a row of two elements.
Copy uncurry add [1 2] == 3
uncurry sub [5 3] == 2
uncurry v2 [1 2] == [1 2]
rowCons
Copy (rowCons e xs)
> e : a
> xs : Row a
> Row a
Prepends an element to a row.
rowCons
creates a new row with the given element as the first element, followed by all elements of the input row.
Copy rowCons 1 [2 3] == [1 2 3]
rowCons b#a [] == [b#a]
rowCons 0 [1] == [0 1]
rowSnoc
Copy (rowSnoc xs e)
> xs : Row a
> e : a
> Row a
Appends an element to a row.
rowSnoc
creates a new row with all elements of the input row, followed by the given element as the last element.
Copy rowSnoc [1 2] 3 == [1 2 3]
rowSnoc [] b#a == [b#a]
rowSnoc [0] 1 == [0 1]
rowApply
Copy (rowApply f xs)
> f : (a > b)
> xs : Row a
> b
Applies a function to a row of arguments.
rowApply
takes a function and a row of arguments, and applies the function to those arguments.
Copy rowApply add [2 3] == 5
rowApply gte [3 4] == 0
rowApply lte [3 4] == 1
rowRepel
Copy (rowRepel f xs)
> f : (a > b)
> xs : Row a
> b
Applies a function to a row of arguments in reverse order.
rowRepel
takes a function and a row of arguments, and applies the function to those arguments in reverse order.
Copy rowRepel add [2 3] == 5
rowRepel gte [3 4] == 1
rowRepel lte [3 4] == 0
slash
Copy (slash xs s e)
> xs : Row a
> s : Nat
> e : Nat
> a
Extracts a slice from a row, from index s
to index e
, padding with zeros if necessary.
Copy slash [1 2 3 4 5] 1 3 == [2 3]
slash [1 2 3] 0 5 == [1 2 3 0 0]
slash [1 2 3] 2 2 == []
slice
Copy (slice xs s e)
> xs : Row a
> s : Nat
> e : Nat
> a
Similar to slash
, but doesn't pad with zeros. It returns a slice from index s
up to (but not including) index e
.
Copy slice [1 2 3 4 5] 1 3 == [2 3]
slice [1 2 3] 0 5 == [1 2 3]
slice [1 2 3] 2 2 == []
chunks
Copy (chunks n xs)
> n : Nat
> xs : Row a
> Row a
Splits a row into chunks of a specified size.
Copy chunks 2 [1 2 3 4 5] == [[1 2] [3 4] [5]]
chunks 3 [1 2 3 4] == [[1 2 3] [4]]
chunks 5 [1 2 3] == [[1 2 3]]
rep
Copy (rep x n)
> x : a
> n : Nat
> Row a
Creates a row by repeating a value a specified number of times.
Copy rep 3 2 == [3 3]
rep b#aaab 3 == [b#aaab b#aaab b#aaab]
rep [] 2 == [[] []]
rowIndexed
Copy (rowIndexed xs)
> xs : Row a
> Row (Nat, a)
Creates a row of pairs, where each pair contains the index and the corresponding element from the input row.
Copy rowIndexed [10 20 30] == [[0 10] [1 20] [2 30]]
rowIndexed [b#aba b#bab] == [[0 b#aba] [1 b#bab]]
rowIndexed [] == []
findIdx
Copy (findIdx f xs d k)
> f : (a > Bool)
> xs : Row a
> d : b
> k : (a > b)
> a
Finds the index of the first element in a row that satisfies a predicate function.
Copy findIdx (lte 5) [1 3 5 7 9] 0 id == 2
findIdx (lte 10) [1 3 5 7 9] 0 id == 0
findIdx even [1 3 5 7] {not found} showNat == {not found}
elemIdx
Copy (elemIdx e xs d k)
> e : a
> xs : Row a
> d : a
> k : a
> a
Finds the index of the first occurrence of a specific element in a row.
Copy elemIdx 5 [1 3 5 7 5] b#{not found} id == 2
elemIdx b#a [b#b b#a b#c] b#{not found} id == 1
elemIdx 4 [1 2 3] b#{not found} id == b#{not found}
has
Copy (has e xs)
> e : a
> xs : Row a
> Bool
Checks if a row contains a specific element.
Copy has 3 [1 2 3 4 5] == 1
has b#a [b#b b#c] == 0
has [] [[1] [2] []] == 1
rowAnd
Copy (rowAnd xs)
> xs : Row Bool
> Bool
Performs a logical AND operation on all elements of a row.
Copy rowAnd [TRUE TRUE FALSE] == 0
rowAnd [TRUE TRUE TRUE] == 1
rowAnd [] == 1
rowOr
Copy (rowOr xs)
> xs : Row Bool
> Bool
Performs a logical OR operation on all elements of a row.
Copy rowOr [FALSE FALSE TRUE] == 1
rowOr [FALSE FALSE FALSE] == 0
rowOr [] == 0
sum
Copy (sum xs)
> xs : Row Nat
> Nat
Calculates the sum of all elements in a row.
Copy sum [1 2 3 4 5] == 15
sum [10 (sub 10 3) 3] == 20
sum [] == 0
sumOf
Copy (sumOf f xs)
> f : (a > Nat)
> xs : Row a
> Nat
Applies a function to each element of a row and then calculates the sum of the results.
Copy sumOf (mul 2) [1 2 3 4] == 20
sumOf (pow 2) [1 2 3] == 14
sumOf id [] == 0
all
Copy (all f xs)
> f : (a > Bool)
> xs : Row a
> Bool
Checks if all elements in a row satisfy a given predicate.
Copy all even [2 4 6 8] == 1
all (lte 0) [1 2 3] == 1
all id [] == 1
any
Copy (any f xs)
> f : (a > Bool)
> xs : Row a
> Bool
Checks if any element in a row satisfies a given predicate.
Copy any odd [2 4 5 8] == 1
any (gte 0) [1 2 3 4] == 0
any id [] == 0
zip
Copy (zip xs ys)
> xs : Row a
> ys : Row b
> Row (a, b)
Combines two rows into a row of pairs.
Copy zip [1 2 3] [b#a b#b b#c] == [[1 b#a] [2 b#b] [3 b#c]]
zip [1 2] [b#a b#b b#c] == [[1 b#a] [2 b#b]]
zip [] [1 2 3] == []
zipWith
Copy (zipWith f xs y)
> f : (a > b > c)
> xs : Row a
> y : Row b
> Row c
Combines two rows using a given function.
Copy zipWith add [1 2 3] [4 5 6] == [5 7 9]
zipWith mul [1 2 3] [1 2 3] == [1 4 9]
zipWith zip [[1 2] [1 2]] [[3 4] [3 4]] == [[[1 3] [2 4]] [[1 3] [2 4]]]
cat
Copy (cat x)
> xs : Row (Row a)
> Row a
Concatenates a row of rows into a single row.
Copy cat [[1 2 3 4] [3 4] [5]] == [1 2 3 4 5]
cat [[] [1 2] [3]] == [1 2 3]
cat [] == []
catMap
Copy (catMap f x)
> f : (a > Row b)
> xs : Row a
> Row b
Applies a function to each element of a row and concatenates the results.
Copy catMap (rep 2) [1 2 3] == [2 2 2 2 2 2]
catMap id [[1 2] [3 4]] == [1 2 3 4]
take
Copy (take n x)
> n : Nat
> xs : Row a
> Row a
Returns the first n elements of a row.
Copy take 3 [1 2 3 4 5] == [1 2 3]
take 2 [b#a b#b] == [b#a b#b]
take 5 [1 2 3] == [1 2 3]
drop
Copy (drop n xs)
> n : Nat
> xs : Row a
> Row a
Removes the first n elements from a row.
Copy drop 2 [1 2 3 4 5] == [3 4 5]
drop 3 [b#a b#b b#c b#d] == [b#d]
drop 5 [1 2 3] == []
rev
Copy (rev xs)
> xs : Row a
> Row a
Reverses the order of elements in a row.
Copy rev [1 2 3 4 5] == [5 4 3 2 1]
rev [b#a b#b b#c b#d] == [b#d b#c b#b b#a]
rev [] == []
span
Copy (span f xs)
> f : (a > Bool)
> xs : Row a
> Row (Row a)
Splits a row into two parts: the longest prefix that satisfies a predicate and the rest.
Copy span (gte 3) [1 2 3 4 1 2 3 4] == [[1 2 3] [4 1 2 3 4]]
span even [2 4 6 7 8 9] == [[2 4 6] [7 8 9]]
span FALSE [1 2 3] == [[] [1 2 3]]
splitAt
Copy (splitAt i xs)
> i : Nat
> xs : Row a
> Row (Row a)
Splits a row at a given index.
Copy splitAt 3 [1 2 3 4 5] == [[1 2 3] [4 5]]
splitAt 0 [1 2 3] == [[] [1 2 3]]
splitAt 5 [1 2 3] == [[1 2 3] []]
intersperse
Copy (intersperse e xs)
> e : a
> xs : Row a
> Row a
Intersperses an element between every element of a row.
Copy intersperse 0 [1 2 3] == [1 0 2 0 3]
intersperse b#a [b#b] == [b#b]
intersperse 0 [] == []
insert
Copy (insert i e xs)
> i : Nat
> e : a
> xs : Row a
> Row a
Inserts an element at a specified index in a row.
Copy insert 1 b#x [b#a b#b b#c] == [b#a b#x b#b b#c]
insert 0 1 [1 2 3] == [1 1 2 3]
insert 3 4 [1 2 3] == [1 2 3 4]
Sorting and Filtering
sort
Copy (sort xs)
> xs : Row a
> Row a
Sorts a row in ascending order.
Copy sort [3 1 4 1 5] == [1 1 3 4 5]
sort [b#c b#a b#b] == [b#a b#b b#c]
sort [] == []
sortBy
Copy (sortBy f xs)
> f : (a > Nat)
> xs : Row a
> Row a
Sorts a row using a custom comparison function.
Copy sortBy (flip cmp) [1 3 2] == [3 2 1]
sortBy (cmp) [[1 2] [] [1]] == [[] [1] [1 2]]
sortOn
Copy (sortOn f xs)
> f : (a > b)
> xs : Row a
> Row b
Sorts a row by applying a function to each element before comparing.
Copy sortOn (even) [1 3 2] == [3 1 2]
sortOn len [[1 2] [3] [4 5 6]] == [[3] [1 2] [4 5 6]]
sortUniq
Copy (sortUniq xs)
> xs : Row a
> Row a
Sorts a row and removes duplicate elements.
Copy sortUniq [3 1 4 1 5 3] == [1 3 4 5]
sortUniq [b#a b#b b#a] == [b#a b#b]
sortUniq [] == []
filter
Copy (filter f xs)
> f : (a > Bool)
> xs : Row a
> Row a
Keeps only the elements of a row that satisfy a predicate.
Copy filter even [1 2 3 4 5] == [2 4]
filter (neq b#a) [b#a b#b b#a b#c] == [b#b b#c]
filter (const TRUE) [1 2 3] == [1 2 3]
delete
Copy (delete e xs)
> e : a
> xs : Row a
> Row a
Removes all occurrences of a value from a row.
Copy delete 3 [1 2 3 4 3 5] == [1 2 4 5]
delete b#a [b#a b#b b#a] == [b#b]
delete 42 [1 2 3] == [1 2 3]
findIdxMany
Copy (findIdxMany f xs)
> f : (a > Bool)
> xs : Row a
> List a
Finds all indices where a predicate is satisfied.
Copy findIdxMany even [1 2 3 4 5 6] == [1 [3 [5 0]]]
findIdxMany (eql b#a) [b#a b#b b#a] == [0 [2 0]]
findIdxMany (const FALSE) [1 2 3] == 0
elemIdxMany
Copy (elemIdxMany e xs)
> e : a
> xs : Row a
> Either (List a) Nat
Finds all indices where a specific element occurs.
Copy elemIdxMany 3 [1 2 3 4 3 5] == [2 [4 0]]
elemIdxMany b#a [b#a b#b b#a] == [0 [2 0]]
elemIdxMany 42 [1 2 3] == 0