Created
September 13, 2011 16:50
-
-
Save fadeev/1214293 to your computer and use it in GitHub Desktop.
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
def cons a, b | |
lambda { |pick| | |
return a if pick == 1 | |
return b if pick == 2 | |
} | |
end | |
def car cons | |
cons.call 1 | |
end | |
def cdr cons | |
cons.call 2 | |
end |
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
def cons a, b | |
lambda { |m| m.call a,b } | |
end | |
def car cons | |
cons.call lambda { |a,b| a } | |
end | |
def cdr cons | |
cons.call lambda { |a,b| b } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment