Created
December 13, 2008 15:06
-
-
Save okkez/35479 to your computer and use it in GitHub Desktop.
Church numerals
This file contains 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
# Church numerals | |
zero = lambda{|f| | |
lambda{|x| x } | |
} | |
one = lambda{|f| | |
lambda{|x| | |
f.call(x) | |
} | |
} | |
two = lambda{|f| | |
lambda{|x| | |
f.call(f.call(x)) | |
} | |
} | |
three = lambda{|f| | |
lambda{|x| | |
f.call(f.call(f.call(x))) | |
} | |
} | |
p zero.call(lambda{|x| x + 1 }).call(0) | |
p one.call(lambda{|x| x + 1 }).call(0) | |
p two.call(lambda{|x| x + 1 }).call(0) | |
p three.call(lambda{|x| x + 1 }).call(0) | |
add = lambda{|a, b| | |
lambda{|f| | |
lambda{|x| | |
a.call(f).call(b.call(f).call(x)) | |
} | |
} | |
} | |
p add.call(two, three).call(lambda{|x| x + 1 }).call(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment