Skip to content

Instantly share code, notes, and snippets.

@DNNX DNNX/cons.rb
Created May 26, 2011

Embed
What would you like to do?
cons/car/cdr in ruby
def cons(x, y)
->(label) do
case label
when :first then x
when :second then y
else raise "Argument is neither :first nor :second -- cons"
end
end
end
def car(tuple)
tuple.call(:first)
end
def cdr(tuple)
tuple.call(:second)
end
pair = cons(1, "a")
puts car(pair) #=> 1
puts cdr(pair) #=> a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.