Skip to content

Instantly share code, notes, and snippets.

@invisiblefunnel
Created September 28, 2011 22:28
Show Gist options
  • Save invisiblefunnel/1249429 to your computer and use it in GitHub Desktop.
Save invisiblefunnel/1249429 to your computer and use it in GitHub Desktop.
Scheme's cons, car, and cdr in Ruby
# 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