Created
July 1, 2013 17:28
-
-
Save bryanwoods/5902820 to your computer and use it in GitHub Desktop.
This file contains 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
class LCVariable < Struct.new(:name) | |
def to_s | |
name.to_s | |
end | |
def inspect | |
to_s | |
end | |
end | |
class LCFunction < Struct.new(:parameter, :body) | |
def to_s | |
"-> #{parameter} { #{body} }" | |
end | |
def inspect | |
to_s | |
end | |
end | |
class LCCall < Struct.new(:left, :right) | |
def to_s | |
"#{left}[#{right}]" | |
end | |
def inspect | |
to_s | |
end | |
end | |
one = LCFunction.new( | |
:p, | |
LCFunction.new( | |
:x, | |
LCCall.new(LCVariable.new(:p), LCVariable.new(:x)) | |
) | |
) | |
increment = LCFunction.new( | |
:n, | |
LCFunction.new( | |
:p, | |
LCFunction.new( | |
:x, | |
LCCall.new( | |
LCVariable.new(:p), | |
LCCall.new( | |
LCCall.new(LCVariable.new(:n), LCVariable.new(:p)), | |
LCVariable.new(:x) | |
) | |
) | |
) | |
) | |
) | |
add = LCFunction.new( | |
:m, | |
LCFunction.new(:n, | |
LCCall.new(LCCall.new(LCVariable.new(:n), increment), LCVariable.new(:m)) | |
) | |
) | |
puts one | |
puts increment | |
puts add | |
# -> p { -> x { p[x] } } | |
# -> n { -> p { -> x { p[n[p][x]] } } } | |
# -> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment