D E F I N I T I O N S
| ||||
| ||||
| Lambda expression using Definitions |
|
Math definition | Lambda model name = definition | Arity #args | Notes and examples | |||
Boolean values and operations | ||||||
True | [true] | \a.\b.a | 0 | Boolean values | ||
False | [false] | \a.\b.b | 0 | |||
Conditional expression | [if] | \c.\t.\e.cte | 3 |
| ||
not | [not] | \a.[if]a[false][true] | 2 | Boolean negation | ||
and | [and] | \a.\b.aba | 2 | Boolean conjunction | ||
or | [or] | \a.\b.aab | 2 | Boolean disjunction | ||
nand | [nand] | \a.\b.[not] (aba) | 2 | not(A and B) | ||
nor | [nor] | \a.\b.[not] (aab) | 2 | not(A or B) | ||
Tuples: having a fixed length | ||||||
pair | [pair] | \a.\b.\f.fab | 2 | Make a tuple (a,b) | ||
First | fst | \p.p(\a.\b.a) | 1 | Select the first (a) from a pair (a,b) | ||
Second | snd | \p.p(\a.\b.b) | 1 | Select the second (b) from a pair (a,b) | ||
Lists of changeable length | ||||||
Nil | [nil] | \l.l(\i.i) | 0 | The empty list | ||
Cons | [cons] | \a.\l.\f.fal | 2 | Construct a list from an element and a list | ||
Head | [head] | \l.l\h.\t.h | 1 | Select the first item from a list | ||
Tail | [tail] | \l.l\h.\t.t | 1 | Select the rest of a list after the first item | ||
isNull | [isnull] | \l.l(\x.\y.\a.\b.a)(\i.i) | 1 | boolean: is a list empty? | ||
Numerals | ||||||
0 | [0] | \f.\x.x | 0 | "Apply f N times to x" Uses a JavaScript function to generate the model | ||
1 | [1] | \f.\x.fx | 0 | |||
2 | [2] | \f.\x.f(fx) | 0 | |||
3 | [3] | \f.\x.f(f(fx)) | 0 | |||
... | [n] | \f.\x.f(f...(fx)..) | 0 | |||
Arithmetic | ||||||
\n.n=0 | [iszero] | \n.n(\y.[false])[true] | 1 | boolean: is a number zero? | ||
\n.n+1 | [succ] | \n.\f.\x.f(nfx) | 1 | the successor function | ||
\n.n-1 | [pred] | \n.[snd](n(\p.[pair]([succ]([fst]p))([fst]p))([pair][0][0])) | 1 | the number before; the predecessor of 0 is 0 | ||
\a.\b.a+b | [sum] | \m.\n.\f.\x.mf(nfx) | 2 | m + n | ||
\a.\b.a×b | [prod] | \m.\n.\f.m(nf) | 2 | m × n | ||
\a.\p.ap | [power] | \m.\n.nm | 2 | m n | ||
\a.\b.|a−b| | [diff] | \m.\n.n[pred]m | 2 | the positive difference between A and B
| ||
Minimum | [min] | \m.\n.[diff]m([diff]mn) | 2 |
| ||
Maximum | [max] | \m.\n.[diff]([sum]mn)([min]mn) | 2 |
| ||
≤ | [le] | \m.\n.[iszero]([diff]mn) | 2 | a ≤ b | ||
≥ | [ge] | \m.\n.[le]nm | 2 | a ≥ b | ||
= | [eq] | \m.\n.[and]([le]mn)([le]nm) | 2 | a = b | ||
≠ | [ne] | \m.\n.[not]([eq]mn)) | 2 | a ≠ b | ||
< | [lt] | \m.\n.[not]([ge]mn) | 2 | a < b | ||
> | [gt] | \m.\n.[not]([le]mn) | 2 | a > b | ||
Recursive definitions | ||||||
Triangular Numbers T(n)=n(n+1)/2 | [tri] | Y(\f.\n.[if]([iszero]n)[0]([sum]n(f f ([pred]n)))) | 1 | T(n) = 1+2+...+n
For example T(3)=6 and [tri][3] reduces to [6] | ||
Mod | [mod] | Y (\f.\m.\n.[if]([lt]mn)m(f f ([diff]mn)n)) | 2 | a (mod b) | ||
is even? | [evenq] | \n.[iszero]([mod]n[2]) | 1 | n (mod 2)==0 | ||
is odd? | [oddq] | \n.[not]([evenq]n) | 1 | n (mod 2) ≠ 0 | ||
÷ | [div] | Y (\f.\m.\n.[if]([lt]mn)[0]([succ](f f ([diff]mn) n))) | 2 | floor(a/b) = integer part of a/b = (a−(a mod b))/b | ||
Factorial: n! | [fact] | Y (\f.\n.[if]([iszero]n)[1]([prod]n(f f ([pred]n)))) | 1 |
n! = 1×2×...×n, if n>0 0! = 1 | ||
IsPrime? | [primeq] | (f f n ([sum][2]d))) [true]
\n.[or]([eq]n[2])([and]([not]([iszero]([modrec][modrec]n[2])))([ptestfrom]n[3])) where ptestfrom = Y (\f.\n.\d.[if] ([le]([prod]dd)n) ([and] ([not] ([iszero]([modrec][modrec]nd))) (ffn([sum]d[2]))) [true]) |
1 |
is n prime? Uses an auxiliary recursive function ptestfromrec(a,b) to check odd numbers from b are not factors of a [primeq][21] -> \a.\b.b = [false] [primeq][19] -> \a.\b.a = [true] | ||
List Processing | ||||||
s, ... ,e | [range] | Y (\f.\s.\e.[if]([gt]es)[nil]([cons]s(ff([succ]s)e)) | 2 | Makes a list of the numbers from s to e | ||
map | [map] | Y (\m.\f.\l.[if]([isnull]l)l([cons](f([head]l))(m m f ([tail]l)))) | 2 |
make a new list by applying a function to each element of a list [map] (W[sum]) ([cons][3]([cons][5][nil])) will map the "doubling" function W[add] = \x.[sum]xx to each of the elements of list cons(3,cons(5,nil)) to give cons(6,cons(10,nil)) | ||
select | [select] | Y (\f.\p.\l.[if]([isnull]l)[nil](p([head]l)([cons]([head]l)(ffp([tail]l))))) | 2 | Select all the elements of a list L that have property P (where P applied to any element gives True for those elements to be selected) [select][evenq]([range][3][6]) -> [cons]4([cons][6][nil]) | ||
take while | [takewhile] | Y ( \w.\p.\l.[if]([isnull]l)[nil]([cons]([head]l)(wwp([tail]l))) | 2 | Construct a new list by taking the elements of a list in turn so long as they meet a given condition [takewhile] [evenq] ([cons][2]([cons][0]([cons][7](cons[4][nil])))) -> [cons][2](cons[0][nil]) | ||
foldLeft | [foldl] | Y (\r.\f.\a.\l.[if]([isnull]l)a(rrf(fa([head]l))([tail]l))) | 3 | accumulate by combining the elements of a list from the left with a given function f and starting value a for example: [foldl] f z ([cons]a([cons]b([cons]c[nil]))) → f(f(fza)b)c | ||
foldRight | [foldr] | Y ( \f.\o.\a.\l.[if]([isnull]l)a(o([head]l)(ffoa([tail]l)) ) | 3 | reduce a list by combining the elements from the right with a given function f and a given (nil) value z for example: [foldr] f z ([cons]a([cons]b([cons]c[nil]))) → fa(fb(fcz)) |