Skip to content

Instantly share code, notes, and snippets.

@DNNX
Created May 26, 2011 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DNNX/992986 to your computer and use it in GitHub Desktop.
Save DNNX/992986 to your computer and use it in GitHub Desktop.
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