Skip to content

Instantly share code, notes, and snippets.

@luctraonmilin
Created April 17, 2014 13:50
Show Gist options
  • Save luctraonmilin/10984924 to your computer and use it in GitHub Desktop.
Save luctraonmilin/10984924 to your computer and use it in GitHub Desktop.
Minimal lambda calculus interpreter
# This can be used as a starting point to build a Scheme or other
# dynamic language interpreter.
module Lambda
def evaluate(expression, environment)
if expression.is_a? Symbol
environment[expression]
elsif expression[0] == "\\"
[expression, environment]
else
apply evaluate(expression[0], environment), evaluate(expression[1], environment)
end
end
def apply(closure, argument)
function, environment = *closure
expression = function[2]
extended_environment = { function[1] => argument }.merge environment
evaluate expression, extended_environment
end
end
include Lambda
function = ["\\", :identity, :identity]
application = [function, :answer]
environment = {
:answer => 42
}
puts evaluate(application, environment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment