Created
September 28, 2011 22:28
-
-
Save invisiblefunnel/1249429 to your computer and use it in GitHub Desktop.
Scheme's cons, car, and cdr in Ruby
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
# A possible implementation of | |
# Scheme's cons, car, and cdr in Ruby | |
def cons x, y | |
lambda do |pick| | |
case pick | |
when 1; x | |
when 2; y | |
end | |
end | |
end | |
def car pair | |
pair.call 1 | |
end | |
def cdr pair | |
pair.call 2 | |
end | |
ex = (cons 3, (cons 4, (cons 5, 6))) | |
p (car (cdr (cdr ex))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment