Created
April 17, 2014 13:50
-
-
Save luctraonmilin/10984924 to your computer and use it in GitHub Desktop.
Minimal lambda calculus interpreter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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