Skip to content

Instantly share code, notes, and snippets.

@dmajda
Created June 8, 2010 09:58
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 dmajda/429831 to your computer and use it in GitHub Desktop.
Save dmajda/429831 to your computer and use it in GitHub Desktop.
# Syntax of Ruby is flexible enough to allow writing Lisp-like code.
# We need few helper functions (no Lisp yet):
def list(*args)
args
end
def car(list)
list.first
end
def cdr(list)
list[1..-1]
end
def print(args)
p args
end
def define(function, body)
name = function.first
args = function[1..-1].map { |a| a.to_s }.join(", ")
eval <<-EOS
def #{name}(#{args})
#{body}
end
EOS
end
# And now Lisp!
(print (car (list 1, 2, 3))) # => 1
(print (cdr (list 1, 2, 3))) # => [2, 3]
(print (car (cdr (list 1, 2, 3)))) # => 2
(define [:square, :x], "x*x")
(print (square 3)) # => 9
# The same in Scheme:
#
# (print (car (list 1 2 3)))
# (print (cdr (list 1 2 3)))
# (print (car (cdr (list 1 2 3))))
#
# (define (square x) (* x x))
# (print (square 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment