Skip to content

Instantly share code, notes, and snippets.

@mattias-lw
Created May 10, 2012 22:16
Show Gist options
  • Save mattias-lw/2656246 to your computer and use it in GitHub Desktop.
Save mattias-lw/2656246 to your computer and use it in GitHub Desktop.
RPN i Ruby
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