Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@emorikawa
Created February 21, 2014 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emorikawa/9139034 to your computer and use it in GitHub Desktop.
Save emorikawa/9139034 to your computer and use it in GitHub Desktop.
Lambda Calculus in Coffeescript
# Combinators
I = (x) -> x
# Church Numerals
int = (n) -> n((x) -> ++x)(0) # Converts Chruch Numeral to literal integer
$0 = ZERO = (f) -> (x) -> x
$1 = ONE = (f) -> (x) -> f x
$2 = TWO = (f) -> (x) -> f f x
$3 = THREE = (f) -> (x) -> f f f x
$4 = FOUR = (f) -> (x) -> f f f f x
$5 = FIVE = (f) -> (x) -> f f f f f x
w = window ? root ? exports
for n in [6..1000]
w["$#{n}"] = (f) -> n=n; (x) -> mem = f(x); mem = f(mem) for i in [0..n]; mem
SUCC = (n) -> (f) -> (x) -> f (n(f)(x))
PLUS = (m) -> (n) -> (f) -> (x) -> m(f)(n(f)(x))
PLUS(ONE)(TWO)
# Church Booleans
b = (f) -> f(true)(false) # Converts Church Boolean to literal 'true' or 'false'
TRUE = (x) -> (y) -> x
FALSE = (x) -> (y) -> y
# Lambda Logic
AND = (p) -> (q) -> p(q)(p)
OR = (p) -> (q) -> p(p)(q)
NOT = (p) -> (a) -> (b) -> p(b)(a)
AND(TRUE)(TRUE) #===> TRUE
b AND(TRUE)(TRUE) #===> true
b AND(TRUE)(FALSE) #===> false
b AND(FALSE)(TRUE) #===> false
b AND(FALSE)(FALSE) #===> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment