Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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
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: (4)
As of 2014-12-28 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls