Builtins
Every builtin is lowerCamelCase and takes the data it works on last, so
partial application reads well: (map (add 1) xs),
(filter (lt 0) xs), (split "," line). Lowercase
names in a signature are type variables. The same reference is built into
the interpreter: run lbd docs [name], or :docs in
the REPL.
Numbers
-
add
Num<a> => a -> a -> a - Sum of two numbers.
Intarithmetic that overflows 64 bits is an error. -
sub
Num<a> => a -> a -> a (sub a b)isa - b.-
mul
Num<a> => a -> a -> a - Product of two numbers.
-
neg
Num<a> => a -> a - Negation.
-
abs
Num<a> => a -> a - Absolute value.
-
pow
Num<a> => a -> Int -> a (pow base exponent); the exponent must not be negative.-
div
Int -> Int -> Int (div a b)isa / brounded toward negative infinity, like Haskell’sdiv; dividing by zero is an error.-
mod
Int -> Int -> Int - Remainder of
div, with the sign of the divisor, like Haskell’smod. -
fdiv
Float -> Float -> Float (fdiv a b)isa / b(IEEE: dividing by zero gives infinity).-
sqrt
Float -> Float - Square root.
-
toFloat
Int -> Float - The
Floatnearest to anInt. -
floor
Float -> Int - Largest integer not greater than the argument; an error for
NaN, infinities and values beyondInt. -
ceil
Float -> Int - Smallest integer not less than the argument.
-
round
Float -> Int - Nearest integer, halves away from zero.
-
min
Ord<a> => a -> a -> a - The smaller of two values; the first if they are equal.
-
max
Ord<a> => a -> a -> a - The larger of two values; the second if they are equal.
Comparison
-
eq
Eq<a> => a -> a -> Bool - Structural equality. Every type without functions or
IOactions in it belongs toEq. -
neq
Eq<a> => a -> a -> Bool - Structural inequality.
-
lt
Ord<a> => a -> a -> Bool (lt a b)isa < b. Every type inEqis also inOrd: data is ordered by constructor, in declaration order, then field by field, and lists element by element.-
le
Ord<a> => a -> a -> Bool (le a b)isa <= b.-
gt
Ord<a> => a -> a -> Bool (gt a b)isa > b.-
ge
Ord<a> => a -> a -> Bool (ge a b)isa >= b.-
compare
Ord<a> => a -> a -> Int -1,0or1asais less than, equal to or greater thanb.
Booleans
Strings
-
concat
String -> String -> String - Joins two strings.
-
show
Show<a> => a -> String - Renders a value as source-like text (strings are quoted). Every type without functions or
IOactions in it belongs toShow. -
parseInt
String -> Result<String, Int> - Parses an integer such as
-42, ignoring surrounding whitespace;Errwith a message when the text is not one. -
parseFloat
String -> Result<String, Float> - Parses a number such as
3.5,2or1e-3, ignoring surrounding whitespace;Errwith a message when the text is not one. -
stringLength
String -> Int - Number of characters in a string.
-
chars
String -> List<String> - Splits a string into its characters.
-
split
String -> String -> List<String> (split delimiter text); the delimiter must not be empty.-
lines
String -> List<String> - Splits text into lines, accepting
\nand\r\n. -
join
String -> List<String> -> String (join separator parts).-
trim
String -> String - Removes leading and trailing whitespace.
Lists
-
length
List<a> -> Int - Number of elements.
-
isEmpty
List<a> -> Bool - Whether the list has no elements.
-
head
List<a> -> Option<a> - First element, or
Nonefor an empty list. -
tail
List<a> -> Option<List<a>> - Everything but the first element, or
Nonefor an empty list.(drop 1 xs)gives the empty list instead. -
get
Int -> List<a> -> Option<a> (get index list), zero-based, orNonewhen the index is out of bounds.-
cons
a -> List<a> -> List<a> - Prepends an element.
-
push
a -> List<a> -> List<a> - Appends an element at the end.
-
remove
Int -> List<a> -> List<a> (remove index list)without the element atindex; the list unchanged when the index is out of bounds, liketakeanddrop.-
append
List<a> -> List<a> -> List<a> - Concatenates two lists.
-
map
(a -> b) -> List<a> -> List<b> - Applies a function to every element.
-
filter
(a -> Bool) -> List<a> -> List<a> - Keeps the elements satisfying a predicate.
-
foldl
(b -> a -> b) -> b -> List<a> -> b - Left fold:
(foldl f z [x, y])is(f (f z x) y). -
foldr
(a -> b -> b) -> b -> List<a> -> b - Right fold:
(foldr f z [x, y])is(f x (f y z)), where each inner fold is evaluated only iffuses it. -
reverse
List<a> -> List<a> - Reverses a list.
-
sort
Ord<a> => List<a> -> List<a> - Stable ascending sort using the same ordering as
lt. -
range
Int -> Int -> List<Int> (range from to)counts fromfromup to, not including,to.-
zipWith
(a -> b -> c) -> List<a> -> List<b> -> List<c> - Combines two lists element-wise, stopping at the shorter.
-
transpose
List<List<a>> -> List<List<a>> - Turns rows into columns, truncated to the shortest row.
-
sum
List<Int> -> Int - Sum of all elements. For floats, use
(foldl add 0.0 xs). -
take
Int -> List<a> -> List<a> - The first
nelements. -
drop
Int -> List<a> -> List<a> - All but the first
nelements.
Tuples
Options and results
-
withDefault
a -> Option<a> -> a (withDefault fallback option)is the value inSome, orfallbackforNone;fallbackis only evaluated when needed.-
mapOption
(a -> b) -> Option<a> -> Option<b> - Applies a function to the value in
Some;NonestaysNone. -
unwrap
Option<a> -> a - The value in
Some; an error forNone. For scripts that know the value is there. -
mapResult
(a -> b) -> Result<e, a> -> Result<e, b> - Applies a function to the value in
Ok; anErrstays as it is. -
toOption
Result<e, a> -> Option<a> Someof the value inOk, orNonefor anErr, dropping the error.-
unwrapOk
Show<e> => Result<e, a> -> a - The value in
Ok; for anErr, an error that shows it (a string without quotes). For scripts that know the value is there.
Input/output
-
pure
a -> IO<a> - An action that does nothing and produces its argument.
-
bind
IO<a> -> (a -> IO<b>) -> IO<b> (bind action f)runsaction, then the actionfreturns for its result.let x <- action in bodyis(bind action \x. body).-
then
IO<a> -> IO<b> -> IO<b> (then first second)runsfirst, discards its result, then runssecond.-
sequence
List<IO<a>> -> IO<List<a>> - Runs a list of actions in order and collects their results.
-
print
String -> IO<Unit> - An action that prints a string. Build other output with
show,concatandjoin:(println (concat "total: " (show n))). -
println
String -> IO<Unit> - Like
print, followed by a newline. -
readFile
String -> IO<String> - An action that reads a whole file. A relative path is relative to the working directory, like a path typed on the command line; start it with
__DIR__to read a file that sits next to the program. -
error
String -> a - Stops the program with an error message when it is evaluated. Not an action: it can occur anywhere.
Source location
-
__FILE__
String - The path of the source file this name is written in;
<repl>in the REPL. -
__DIR__
String - The directory of the source file this name is written in, which
usepaths are relative to;.in the REPL.(readFile (concat __DIR__ "/data.txt"))reads thedata.txtnext to the program from any working directory. -
__LINE__
Int - The line this name is written on, counting from 1.
No builtin matches that name.