Created
November 30, 2012 17:11
-
-
Save b0oh/4177081 to your computer and use it in GitHub Desktop.
Church Numerals on CoffeeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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