Lambda Discipline

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. Int arithmetic that overflows 64 bits is an error.
sub Num<a> => a -> a -> a
(sub a b) is a - 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) is a / b rounded toward negative infinity, like Haskell’s div; dividing by zero is an error.
mod Int -> Int -> Int
Remainder of div, with the sign of the divisor, like Haskell’s mod.
fdiv Float -> Float -> Float
(fdiv a b) is a / b (IEEE: dividing by zero gives infinity).
sqrt Float -> Float
Square root.
toFloat Int -> Float
The Float nearest to an Int.
floor Float -> Int
Largest integer not greater than the argument; an error for NaN, infinities and values beyond Int.
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 IO actions in it belongs to Eq.
neq Eq<a> => a -> a -> Bool
Structural inequality.
lt Ord<a> => a -> a -> Bool
(lt a b) is a < b. Every type in Eq is also in Ord: 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) is a <= b.
gt Ord<a> => a -> a -> Bool
(gt a b) is a > b.
ge Ord<a> => a -> a -> Bool
(ge a b) is a >= b.
compare Ord<a> => a -> a -> Int
-1, 0 or 1 as a is less than, equal to or greater than b.

Booleans

not Bool -> Bool
Boolean negation.
and Bool -> Bool -> Bool
Conjunction; the second argument is only evaluated when needed.
or Bool -> Bool -> Bool
Disjunction; the second argument is only evaluated when needed.
if Bool -> a -> a -> a
(if c t e) evaluates only the chosen branch.

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 IO actions in it belongs to Show.
parseInt String -> Result<String, Int>
Parses an integer such as -42, ignoring surrounding whitespace; Err with a message when the text is not one.
parseFloat String -> Result<String, Float>
Parses a number such as 3.5, 2 or 1e-3, ignoring surrounding whitespace; Err with 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 \n and \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.
tail List<a> -> Option<List<a>>
Everything but the first element, or None for an empty list. (drop 1 xs) gives the empty list instead.
get Int -> List<a> -> Option<a>
(get index list), zero-based, or None when 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 at index; the list unchanged when the index is out of bounds, like take and drop.
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 if f uses 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 from from up 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 n elements.
drop Int -> List<a> -> List<a>
All but the first n elements.

Tuples

fst (a, b) -> a
The first component of a pair.
snd (a, b) -> b
The second component of a pair.

Options and results

withDefault a -> Option<a> -> a
(withDefault fallback option) is the value in Some, or fallback for None; fallback is only evaluated when needed.
mapOption (a -> b) -> Option<a> -> Option<b>
Applies a function to the value in Some; None stays None.
unwrap Option<a> -> a
The value in Some; an error for None. 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; an Err stays as it is.
toOption Result<e, a> -> Option<a>
Some of the value in Ok, or None for an Err, dropping the error.
unwrapOk Show<e> => Result<e, a> -> a
The value in Ok; for an Err, 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) runs action, then the action f returns for its result. let x <- action in body is (bind action \x. body).
then IO<a> -> IO<b> -> IO<b>
(then first second) runs first, discards its result, then runs second.
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, concat and join: (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 use paths are relative to; . in the REPL. (readFile (concat __DIR__ "/data.txt")) reads the data.txt next to the program from any working directory.
__LINE__ Int
The line this name is written on, counting from 1.