Created
May 10, 2012 22:16
-
-
Save mattias-lw/2656246 to your computer and use it in GitHub Desktop.
RPN i Ruby
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
def rpn(expr) | |
head, tail = expr.first, expr[1..-1] | |
case head | |
when nil | |
return lambda { |n| n } | |
when /\d+/ | |
rpn(tail).call(Integer(head)) | |
else | |
rest = rpn(tail) | |
lambda { |n| | |
lambda { |m| | |
rest.call(m.send(expr[0].to_sym, n)) | |
} | |
} | |
end | |
end | |
input = ARGV[0].split | |
puts rpn(input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment