Numbers in Marigold are represented using Church Numerals. They represent natural numbers, meaning there are only positive numbers, with no decimals.
To represent a number n, you create a lambda that takes in a function f and value x,
and then apply f to x n times.
SUCC (short for successor) is a function that takes a number, and adds one to it.
SUCC [number]
or more commonly
[number]++
@n.f (n f x)
ADD is a function that takes in two numbers and returns their sum.
ADD [number] [number]
or more commonly
[number] + [number]
@mn.@fx.m f (n f x)
PRED (short for predecessor) is a function that takes in a number and subtracts one.
PRED [number]
or more commonly
[number]--
PRED uses pairs to work
@n.SECOND (n Φ (PAIR 0 0))
where Φ = @p.PAIR (SUCC (SECOND p)) (SECOND p)
SUB (short for subtract) is a function that takes in a number and subtracts one.
SUB [number] [number]
or more commonly
[number] - [number]
@mn.n PRED m
ISZERO takes in a number and determines if it is equal to zero
ISZERO [number]
@n.n (@x.false) true
EQ (short for equals) is a function that determines if two numbers are equal
EQ [number] [number]
or more commonly
[number] == [number]
@mn.AND (ISZERO (SUB a b)) (ISZERO (SUB b a))
LT (short for less than) determines if one number is less than another
LT [number] [number]
or more commonly
[number] < [number]
@mn.AND (ISZERO (SUB a b)) (NOT (ISZERO (SUB b a)))
LTE (short for less than or equal to) determines if one number is less than or equal to another
LTE [number] [number]
or more commonly
[number] <= [number]
@mn.(ISZERO (SUB a b))
GT (short for greater than) determines if one number is greater than another
GT [number] [number]
or more commonly
[number] > [number]
@mn.AND (NOT (ISZERO (SUB a b))) (ISZERO (SUB b a))
GTE (short for greater than or equal to) determines if one number is greater than or equal to another
GTE [number] [number]
or more commonly
[number] >= [number]
@mn.(ISZERO (SUB b a))
MULT (short for multiply) is a function that multiplies two numbers
MULT [number] [number]
or more commonly
[number] * [number]
@mn.m (ADD n) 0
DIV (short for divide) is a function that floor divides one number by another.
DIV [number] [number]
or more commonly
[number] // [number]
@ab.FIRST (a (@p.EQ (SECOND p) (PRED b) (PAIR (SUCC (FIRST p)) 0) (PAIR (FIRST p) (SUCC (SECOND p))) ) (PAIR 0 0))
MOD (short for modulo) is a function that returns one number mod another.
MOD [number] [number]
or more commonly
[number] % [number]
@ab.SECOND (a (@p.EQ (SECOND p) (PRED b) (PAIR (SUCC (FIRST p)) 0) (PAIR (FIRST p) (SUCC (SECOND p))) (PAIR 0 0)))
(Notice how the only difference between div and mod is that the inner function returns a pair that they access different elements of. That’s because the pair is the quotient first and the remainder second)