Skip to content

Instantly share code, notes, and snippets.

@b0oh
Created November 30, 2012 17:11
Show Gist options
  • Select an option

  • Save b0oh/4177081 to your computer and use it in GitHub Desktop.

Select an option

Save b0oh/4177081 to your computer and use it in GitHub Desktop.
Church Numerals on CoffeeScript
# helpers
inc = (x) -> x + 1
church = (n) ->
if n > 0
succ church n-1
else
zero
unchurch = (n) ->
n(inc) 0
# epic starts here
# numerals
succ = (n) -> (f) -> (x) -> f n(f) x
plus = (m) -> (n) -> (f) -> (x) -> m(f) n(f) x
mult = (m) -> (n) -> (f) -> n m f
exp = (m) -> (n) -> n m
is_zero = (n) -> (n (x) -> false) true
zero = (f) -> (x) -> x
one = (f) -> (x) -> f x # succ zero
two = (f) -> (x) -> f f x # succ succ zero
alert unchurch (f) -> (x) -> f f f x
alert unchurch church 9
alert unchurch succ church 9
alert unchurch plus(church 7) church(4)
alert is_zero zero
alert is_zero two
alert unchurch exp(two) succ(two)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment