Re: RPN calculator in Perl 6

2009-06-08 Thread Daniel Ruoso
Em Sáb, 2009-06-06 às 19:51 +0200, Daniel Carrera escreveu:
> 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.

Fixed now!
http://sial.org/pbot/37088

(with a op trace included, so one can see what's happening...)

> Are you planning to write a post explaining how your program works?

Maybe, but if you want to beat me to it, feel free ;)

> I figure that the explanation is as useful as the example. I sure
> spent a lot of time writing the blog post.

I'm not sure I'll have the time to write it soon, but it will certainly
be on my list, unless you kindly post about it first ;)

daniel



Re: RPN calculator in Perl 6

2009-06-08 Thread Daniel Ruoso
Em Sáb, 2009-06-06 às 18:22 +0200, Daniel Carrera escreveu:
> Daniel Ruoso wrote:
> > er... that's because I didn't tried to implement it... but it
> certainly
> > is possible to, just by declaring the :(@a, Num $a) variant...
> Well, * is implemented, so I guess you are talking about complex 
> expressions.

Yes... that's what wasn't implemented... But now it is ;)

http://sial.org/pbot/37085

daniel



Re: RPN calculator in Perl 6

2009-06-08 Thread Daniel Ruoso
Em Sáb, 2009-06-06 às 14:06 +0200, Daniel Carrera escreveu:
> I just wrote a blog post showing how to make a reverse polish notation 
> calculator in Perl 6. In the process I show some of Perl 6's grammar 
> features.

TIMTOWTDI ;)

http://sial.org/pbot/37075

daniel



Re: RPN calculator in Perl 6

2009-06-08 Thread Geoffrey Broadwell
On Sat, 2009-06-06 at 13:18 -0300, Daniel Ruoso wrote: 
> http://sial.org/pbot/37077
> A slightly improved syntax, as per jnthn++ suggestion...

My list mail has been very delayed, so this may be out of sequence, but
in case no one mentioned it yet:

http://sial.org/pbot/37102

(That's ruoso++'s later 37100 paste with a couple small tweaks by me.)

I wanted to shrink that even further by replacing the given/when with a
direct call of the correct op variant, but I couldn't figure out how to
do that in current Rakudo.  (And just using eval inside a multi seemed
to be broken.)


-'f




Re: RPN calculator in Perl 6

2009-06-08 Thread Daniel Ruoso
Em Dom, 2009-06-07 às 00:07 +0200, Daniel Carrera escreveu:
> Daniel Ruoso wrote:
> >> Are you planning to write a post explaining how your program works?
> > Maybe, but if you want to beat me to it, feel free ;)
> >> I figure that the explanation is as useful as the example. I sure
> >> spent a lot of time writing the blog post.
> > I'm not sure I'll have the time to write it soon, but it will certainly
> > be on my list, unless you kindly post about it first ;)
> Honestly, I don't really know how your version works.

I'll take the code as in http://sial.org/pbot/37089
Feel free to post it if you like...

Ok, I think I can skip the first part, where I declare a token Num,
since your example explain it in great detail.

At first I'm using multi subs, the declaration is just

 multi name ($a, $b) {...}

and is just a shortcut for

 our multi sub name ($a, $b) {...}

which means we're declaring a sub named '&name' which will also be
registered in the current package (our), with the signature inside the
parens. This will work mostly as multis work in other languages, it will
invoke the variant which signature matches the parameters.

The second point is the use of the "infix:" name in the multi. This
is how you declare new operators in Perl 6. infix describe the syntatic
category of the operator, in this specific case, it means the operator
stays between two values. So, after defining the multi sub, I could just
use:

 5 rpn 4

and this would invoke

 infix:(5, 4)

If you're following until here, you probably realize that I could also
do

 ((5 rpn 4) rpn '+') rpn 3

but Perl 6 has the "reduce meta operator", which takes any infix
operator and builds the above grouping for an arbitrary list, and that's
the last line of the code

  [rpn] @*ARGS[0].words;

This takes the list of words in @*ARGS[0] (which means I require a space
between every token) and reduces it using the infix: operator,
which is implemented by each of the variants in the code.

The reduce meta-op works by taking the first two elements of the list
and running the infix: with them as parameters, take the result of
that and the next element of the list and call it again, then do it
again until the list ends.

The last thing you need to know to understand this is about subsets, as
used in the signature of most variants

 multi infix: ($a where //, $b where //) {...}

This signature will take two arguments where both must match the Num
token. This happens to be the variant that matches the start of the
list, and it is the only one that doesn't expect an Array as the first
argument, because it will itself return an Array with the two numbers.
As the reduce metaop takes the result from this call and the next
element on the list to call the sub again, the next call will receive an
array with the two first numbers as the first argument and the next
token as second argument, then the signature match does its trick and we
go on until we reach the end of the list, and the last calculation is
made.

This solution is mostly a functional solution. Probably if you ask the
LISP guys to work on a program to do RPN, it will probably look like
this...

daniel



Re: RPN calculator in Perl 6

2009-06-08 Thread Daniel Ruoso
Em Sáb, 2009-06-06 às 19:45 -0400, Minimiscience escreveu:
> my $ls = @a.shift;
> my $rs = @a.shift;
> To:
> my $rs = @a.pop;
> my $ls = @a.pop;

Thanks... this was already solved in the latest version I sent

http://sial.org/pbot/37089

daniel



Re: RPN calculator in Perl 6

2009-06-08 Thread Daniel Ruoso
http://sial.org/pbot/37077
A slightly improved syntax, as per jnthn++ suggestion...

Em Sáb, 2009-06-06 às 18:08 +0200, Daniel Carrera escreveu:
> Daniel Carrera wrote
> > Ok, try again:
> > % perl6 rpn.pl "2 2+"
> > 2 2
> You can probably fix that with a different split() line. I tried using 
>  instead of \s+ but the program just hangs forever.

Hmm.. it certainly is in the split, I'm not sure how to get the barrier
between the 2 and the +

> I also tried a more complex expression, and the calculator didn't like it:
> % perl6 rpn.pl "5 4 + 3 / 5 3 - *"
> Error parsing expression near *

er... that's because I didn't tried to implement it... but it certainly
is possible to, just by declaring the :(@a, Num $a) variant...

daniel



Re: RPN calculator in Perl 6

2009-06-06 Thread Minimiscience
[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:(@a, $op where ...) operators call .shift  
on the arrays to get the operands, yet the infix:(... , $b where / 
/) 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:() 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


Re: RPN calculator in Perl 6

2009-06-06 Thread Daniel Carrera

Daniel Ruoso wrote:

Are you planning to write a post explaining how your program works?


Maybe, but if you want to beat me to it, feel free ;)


I figure that the explanation is as useful as the example. I sure
spent a lot of time writing the blog post.


I'm not sure I'll have the time to write it soon, but it will certainly
be on my list, unless you kindly post about it first ;)


Honestly, I don't really know how your version works.

Daniel.


Re: RPN calculator in Perl 6

2009-06-06 Thread Daniel Carrera

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.

Are you planning to write a post explaining how your program works? I 
figure that the explanation is as useful as the example. I sure spent a 
lot of time writing the blog post.


Cheers,
Daniel.


Re: RPN calculator in Perl 6

2009-06-06 Thread Daniel Carrera

Daniel Ruoso wrote:
You can probably fix that with a different split() line. I tried using 
 instead of \s+ but the program just hangs forever.


Hmm.. it certainly is in the split, I'm not sure how to get the barrier
between the 2 and the +


Does Perl 6 still have the word barrier \b regex? Maybe some sort of 
character class that matches spaces and word boundaries? I dunno...




I also tried a more complex expression, and the calculator didn't like it:
% perl6 rpn.pl "5 4 + 3 / 5 3 - *"
Error parsing expression near *


er... that's because I didn't tried to implement it... but it certainly
is possible to, just by declaring the :(@a, Num $a) variant...


Well, * is implemented, so I guess you are talking about complex 
expressions.


Daniel.


Re: RPN calculator in Perl 6

2009-06-06 Thread Daniel Carrera

Daniel Carrera wrote

Ok, try again:

% perl6 rpn.pl "2 2+"
2 2


You can probably fix that with a different split() line. I tried using 
 instead of \s+ but the program just hangs forever.


I also tried a more complex expression, and the calculator didn't like it:

% perl6 rpn.pl "5 4 + 3 / 5 3 - *"
Error parsing expression near *

:-(

Daniel.


Re: RPN calculator in Perl 6

2009-06-06 Thread Daniel Carrera

Daniel Carrera wrote:

http://sial.org/pbot/37075


 % perl rpn.pl "2 2 +"


Tee hee... that should have been "perl6". :-)

Ok, try again:

% perl6 rpn.pl "2 2+"
2 2




Re: RPN calculator in Perl 6

2009-06-06 Thread Daniel Carrera

Daniel Ruoso wrote:

TIMTOWTDI ;)


The objective of the blog was more about the learning + teaching 
experience than anything else.




http://sial.org/pbot/37075



 % perl rpn.pl "2 2 +"
Semicolon seems to be missing at rpn.pl line 2.
String found where operator expected at rpn.pl line 13, near "where '+'"
(Do you need to predeclare where?)
Array found where operator expected at rpn.pl line 14, near "] "
(Missing operator before  ?)
Scalar found where operator expected at rpn.pl line 14, near "@($a"
(Missing operator before $a?)
String found where operator expected at rpn.pl line 17, near "where '-'"
(Do you need to predeclare where?)
Array found where operator expected at rpn.pl line 18, near "] "
(Missing operator before  ?)
Scalar found where operator expected at rpn.pl line 18, near "@($a"
(Missing operator before $a?)
String found where operator expected at rpn.pl line 21, near "where '*'"
(Do you need to predeclare where?)
Array found where operator expected at rpn.pl line 22, near "*] "
(Missing operator before  ?)
Scalar found where operator expected at rpn.pl line 22, near "@($a"
(Missing operator before $a?)
Semicolon seems to be missing at rpn.pl line 24.
String found where operator expected at rpn.pl line 25, near "where '/'"
(Do you need to predeclare where?)
Semicolon seems to be missing at rpn.pl line 28.
String found where operator expected at rpn.pl line 30, near "fail 
'Error parsing expression near '"

(Do you need to predeclare fail?)
Array found where operator expected at rpn.pl line 33, near "] "
(Missing operator before  ?)
Bareword found where operator expected at rpn.pl line 33, near "@*ARGS"
(Missing operator before ARGS?)
Operator or semicolon missing before *ARGS at rpn.pl line 33.
Ambiguous use of * resolved as operator * at rpn.pl line 33.
syntax error at rpn.pl line 1, near "\."
Missing right curly or square bracket at rpn.pl line 34, at end of line
Execution of rpn.pl aborted due to compilation errors.


RPN calculator in Perl 6

2009-06-06 Thread Daniel Carrera

Hi all,

I just wrote a blog post showing how to make a reverse polish notation 
calculator in Perl 6. In the process I show some of Perl 6's grammar 
features.


Someone in IRC thought it was good, so I decided to post here in the off 
chance that someone else finds it interesting or useful.


http://daniel.carrera.bz/2009/06/rpn-calculator-in-perl-6/

Cheers,
Daniel.