Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix: Built-in Functions

This appendix covers built-in functions for basic operations. For numerical linear algebra (eigenvalues, SVD, etc.), see LAPACK Functions.

Output Functions

Functions for displaying values:

FunctionAliasesDescription
out(x)show(x), print(x)Pretty-print value and return it

Example

out([[1, 2], [3, 4]])
// Prints:
// ┌      ┐
// │ 1  2 │
// │ 3  4 │
// └      ┘

Arithmetic Functions

FunctionAliasesDescriptionExample
negate(x)Unary negationnegate(5)-5
abs(x)fabsAbsolute valueabs(-3)3
sqrt(x)Square rootsqrt(16)4
pow(x, y)powerx^ypow(2, 3)8
floor(x)Round downfloor(3.7)3
ceil(x)ceilingRound upceil(3.2)4
round(x)Round to nearestround(3.5)4
trunc(x)truncateTruncate toward zerotrunc(-3.7)-3
frac(x)fractFractional partfrac(3.7)0.7
sign(x)signumSign (-1, 0, or 1)sign(-5)-1
min(x, y)Minimummin(3, 7)3
max(x, y)Maximummax(3, 7)7
mod(x, y)fmod, remainderModulo/remaindermod(7, 3)1
hypot(x, y)√(x² + y²) stablehypot(3, 4)5

Trigonometric Functions (radians)

All trigonometric functions use radians, not degrees. Use radians(deg) to convert.

FunctionAliasesDescriptionExample
sin(x)Sinesin(0)0
cos(x)Cosinecos(0)1
tan(x)Tangenttan(0)0
asin(x)arcsinArcsineasin(1)π/2
acos(x)arccosArccosineacos(1)0
atan(x)arctanArctangentatan(1)π/4
atan2(y, x)arctan22-arg arctangentatan2(1, 1)π/4
radians(deg)deg_to_radDegrees to radiansradians(180)π

Hyperbolic Functions

FunctionAliasesDescription
sinh(x)Hyperbolic sine
cosh(x)Hyperbolic cosine
tanh(x)Hyperbolic tangent
asinh(x)arcsinhInverse hyperbolic sine
acosh(x)arccoshInverse hyperbolic cosine
atanh(x)arctanhInverse hyperbolic tangent

Identity: cosh(x)² - sinh(x)² = 1

Exponential and Logarithmic

FunctionAliasesDescriptionExample
exp(x)e^xexp(1)2.718...
exp2(x)2^xexp2(3)8
log(x)lnNatural logarithmlog(e())1
log10(x)Base-10 logarithmlog10(100)2
log2(x)Base-2 logarithmlog2(8)3

List Operations

Basic List Functions

FunctionAliasesDescriptionExample
Cons(x, xs)consPrepend elementCons(1, Nil)
NilnilEmpty listNil
head(xs)carFirst elementhead([1,2,3])1
tail(xs)cdrRest of listtail([1,2,3])[2,3]
length(xs)list_lengthList lengthlength([1,2,3])3
nth(xs, n)list_nthGet nth element (0-indexed)nth([1,2,3], 1)2

List Literal Syntax

[1, 2, 3]           // Bracket list (preferred for numeric work)
[]                  // Empty list

List Generation

FunctionDescriptionExample
range(n)Integers 0 to n-1range(4)[0, 1, 2, 3]
range(start, end)Integers from start to end-1range(2, 5)[2, 3, 4]
linspace(start, end)50 evenly spaced floatslinspace(0, 1)[0, 0.0204..., ...]
linspace(start, end, n)n evenly spaced floatslinspace(0, 1, 5)[0, 0.25, 0.5, 0.75, 1]

Higher-Order List Functions

These functions take a lambda as their first argument.

FunctionAliasesDescription
list_map(f, xs)Apply f to each element
list_filter(pred, xs)Keep elements where pred returns true
list_fold(f, init, xs)Left fold with accumulator
list_flatmap(f, xs)flatmap, concat_mapMap then flatten results
list_zip(xs, ys)Pair corresponding elements

list_map

Apply a function to each element:

list_map(lambda x . x * 2, [1, 2, 3])
// → [2, 4, 6]

list_map(lambda x . x * x, range(5))
// → [0, 1, 4, 9, 16]

list_filter

Keep elements satisfying a predicate:

list_filter(lambda x . x > 2, [1, 2, 3, 4, 5])
// → [3, 4, 5]

list_fold

Reduce a list with an accumulator (left fold):

// Sum: f(f(f(0, 1), 2), 3) = ((0+1)+2)+3 = 6
list_fold(lambda acc x . acc + x, 0, [1, 2, 3])
// → 6

// Product
list_fold(lambda acc x . acc * x, 1, [2, 3, 4])
// → 24

list_flatmap

Map a function that returns lists, then flatten:

list_flatmap(lambda x . [x, x*10], [1, 2, 3])
// → [1, 10, 2, 20, 3, 30]

list_zip

Pair corresponding elements (stops at shorter list):

list_zip([1, 2, 3], ["a", "b", "c"])
// → [Pair(1, "a"), Pair(2, "b"), Pair(3, "c")]

Use fst and snd to extract pair components:

let p = Pair(1, "a") in fst(p)  // → 1
let p = Pair(1, "a") in snd(p)  // → "a"

List Manipulation

FunctionAliasesDescriptionExample
list_concat(xs, ys)list_appendConcatenate two listslist_concat([1,2], [3,4])[1,2,3,4]
list_flatten(xss)list_joinFlatten nested listlist_flatten([[1,2], [3,4]])[1,2,3,4]
list_slice(xs, start, end)Sublist from start to end-1list_slice([a,b,c,d], 1, 3)[b,c]
list_rotate(xs, n)Rotate left by n positionslist_rotate([a,b,c], 1)[b,c,a]

String Operations

FunctionDescriptionExample
concat(a, b)Concatenate stringsconcat("hello", "world")"helloworld"
strlen(s)String lengthstrlen("hello")5
contains(s, sub)Check substringcontains("hello", "ell")true
substr(s, start, len)Extract substringsubstr("hello", 1, 3)"ell"
replace(s, old, new)Replace substringreplace("hello", "l", "L")"heLLo"

Matrix Operations (Basic)

For advanced operations (eigenvalues, SVD), see LAPACK Functions.

Matrix Creation

FunctionAliasesDescriptionExample
matrix(rows, cols, elements)Create matrixmatrix(2, 2, [1,2,3,4])
eye(n)identity(n)Identity matrixeye(3)
zeros(m, n)Zero matrixzeros(2, 3)
ones(m, n)Matrix of onesones(2, 3)
diag_matrix(elements)diagonalDiagonal matrixdiag_matrix([1,2,3])

Matrix Literals

[[1, 2, 3],
 [4, 5, 6]]         // 2×3 matrix

Matrix Properties

FunctionAliasesDescription
size(A)shape, dimsDimensions [rows, cols]
nrows(A)num_rowsNumber of rows
ncols(A)num_colsNumber of columns

Element Access

FunctionAliasesDescription
matrix_get(A, i, j)elementGet element at (i, j)
matrix_row(A, i)rowGet row i
matrix_col(A, j)colGet column j
matrix_diag(A)diagGet diagonal

Element Modification

FunctionDescription
set_element(A, i, j, val)Set element at (i, j)
set_row(A, i, row)Set row i
set_col(A, j, col)Set column j
set_diag(A, diag)Set diagonal

Basic Arithmetic

FunctionAliasesDescription
matrix_add(A, B)builtin_matrix_addA + B
matrix_sub(A, B)builtin_matrix_subA - B
multiply(A, B)matmul, builtin_matrix_mulA × B
scalar_matrix_mul(c, A)builtin_matrix_scalar_mulc × A
transpose(A)builtin_transposeAᵀ
trace(A)builtin_tracetr(A)
det(A)builtin_determinantdet(A)

Matrix Stacking

FunctionAliasesDescription
vstack(A, B)append_rowsStack vertically
hstack(A, B)append_colsStack horizontally
prepend_row(A, row)Add row at top
append_row(A, row)Add row at bottom
prepend_col(A, col)Add column at left
append_col(A, col)Add column at right

Mathematical Constants

FunctionUnicodeValueDescription
pi()π3.14159…Pi
e()2.71828…Euler’s number
tau()τ6.28318…τ = 2π
i√(-1)Imaginary unit

Note: pi(), e(), and tau() are zero-argument functions.

Boolean Constants

ConstantDescription
True / trueBoolean true
False / falseBoolean false

See Also