[I just realized I sent this directly to Daniel rather than to the list, so for the benefit of onlookers...]

On Jun 6, 2009, at 1:51 PM, Daniel Carrera wrote:
Daniel Ruoso wrote:
Yes... that's what wasn't implemented... But now it is ;)
http://sial.org/pbot/37085

Close, but...

% perl6 rpn.pl "5 4 + 3 / 5 3 - *"
-6

That should be a positive 6.

I think I see the problem (assuming it hasn't already been pointed out off-list). The infix:<rpn>(@a, $op where ...) operators call .shift on the arrays to get the operands, yet the infix:<rpn>(... , $b where / <Num>/) operators place the numbers on the *end* of the stack, where the next reverse-polish operator should read them from. Thus, when the '-' is processed, the stack is "3 5 3", where the first three is the result of "5 4 + 3 /", and the first two numbers are shifted off for subtraction rather than the last two. In order to fix this, either change the first two infix:<rpn>() functions so that they place new numbers at the *beginning* of the array, or else change each occurrence of

   my $ls = @a.shift;
   my $rs = @a.shift;

To:

   my $rs = @a.pop;
   my $ls = @a.pop;

(Note that, in addition to the use of .pop, $rs is now acquired before $ls.)

At least, I think that's how it works.

-- Minimiscience

Reply via email to