Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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?
[Discipulus]: my ($need,$opta,$optb ) = @_; .. if $opta..
[shmem]: all else leads to trouble, even if the third argument depends on the existence of the second. That may become brittle.
[Discipulus]: but if have case like subname(15,undef,3 ) maybe bettere named parameters
[Lady_Aleena]: I don't want to have to do: alpha_menu($hash, undef, $type);
[Lady_Aleena]: Or what Discipulus said.
[shmem]: Lady_Aleena: geany supports ctags.
[Discipulus]: a good compromise can be my ($need, $opts_ref) = @_ a scalar and an hash reference
[Discipulus]: see you monks!
[Lady_Aleena]: shmem, let me get this sub rewritten, then I will look into how to use ctags in geany. Deal? 8)
[shmem]: Discipulus: yeah, that might eventually prepare the path for OO ;-)

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2017-04-27 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (506 votes). Check out past polls.