Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Tales from writing a RPN evaluator in Perl 5, Perl 6 and Haskell

by Anonymous Monk
on Jan 13, 2006 at 03:58 UTC ( #522877=note: print w/ replies, xml ) Need Help??


in reply to Tales from writing a RPN evaluator in Perl 5, Perl 6 and Haskell

Here's a Ruby version:

def evaluate expr stack = [] expr.split(/\s+/).each do |token| stack << case token when /^-?\d*$/ token.to_i when /^[+\-*\/]$/ right, left = stack.pop, stack.pop raise "Stack underflow" unless left and right left.send token, right else raise "Invalid token: #{token}" end end stack.length == 1 or raise "Invalid stack: #{stack}" stack[0] end


Comment on Re: Tales from writing a RPN evaluator in Perl 5, Perl 6 and Haskell
Download Code
Replies are listed 'Best First'.
Re^2: Tales from writing a RPN evaluator in Perl 5, Perl 6 and Haskell
by audreyt (Hermit) on Jan 14, 2006 at 17:18 UTC
    That's pretty nice... Here is a straight port from Ruby to Perl6:
    sub evaluate ($expr) { my @stack; for $expr.split { @stack.push: do { when /^ -?\d* $/ { $_ } when <+ - * /> { my $right = @stack.pop; my $left = @stack.pop; defined($left & $right) or fail "Stack underflow"; $left.$_: $right; } fail "Invalid token: $_"; } } @stack == 1 or fail "Invalid stack: @stack"; @stack[0]; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://522877]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (15)
As of 2015-07-31 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls