Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

error when using -> as lambda function

5 views
Skip to first unread message

Wim Vanderbauwhede

unread,
Oct 3, 2006, 10:22:48 AM10/3/06
to perl6...@perl.org
Hi all,

I encountered this strange error in pugs (in 6.2.11,6.2.12 and the latest
svn)

My first program (lambda1.p6) calculates the factorial of 5 using -> as
lambda functions:

say (-> $n { -> $f { $f($n,$f) }.( -> $n, $f { $n<2 ?? 1 !! $n*$f($n-1,$f)
}) }).(5);
say "OK";
say "OK";

$ pugs lambda1.p6
120
OK
OK

-----
The second program (lambda2.p6) has the first line repeated as a comment:

say (-> $n { -> $f { $f($n,$f) }.( -> $n, $f { $n<2 ?? 1 !! $n*$f($n-1,$f)
}) }).(5);
say "OK";
#say (-> $n { -> $f { $f($n,$f) }.( -> $n, $f { $n<2 ?? 1 !! $n*$f($n-1,$f)
}) }).(5);
say "OK";

$ pugs lambda2.p6
***
Unexpected "$"
expecting comment, operator, statement modifier, ";" or "}"
at lambda2.p6 line 3, column 10

So the comment causes an error!
-----
If I uncomment that line, I still get the same error (lambda3.p6):

$ pugs lambda3.p6
***
Unexpected "$"
expecting comment, operator, statement modifier, ";" or "}"
at lambda3.p6 line 3, column 9

The error is now at column 9 i.o. column 10.

Could anyone shed any light on what's going on here?

pugs Version: 6.2.12 (r13798)
perl v5.8.7
parrot 0.4.6

Thanks!

Wim

--
If it's pointless, what's the point?
If there is a point to it, what's the point?
(Tibor Fischer, "The Thought Gang")

Audrey Tang

unread,
Oct 3, 2006, 11:30:31 AM10/3/06
to Wim Vanderbauwhede, perl6...@perl.org

在 Oct 3, 2006 10:22 PM 時,Wim Vanderbauwhede 寫到:

> say (-> $n { -> $f { $f($n,$f) }.( -> $n, $f { $n<2 ?? 1 !! $n*$f
> ($n-1,$f)

> }) }).(5);
> say "OK";
> #say (-> $n { -> $f { $f($n,$f) }.( -> $n, $f { $n<2 ?? 1 !! $n*$f
> ($n-1,$f)
> }) }).(5);
> say "OK";

It's extremely subtle -- $n<2 should never have parsed in the first
place, because it's always a
hash dereference (as in $n<2>); put a whitespace before the < sign to
get the infix comparison
operator you're looking for.

See L<S03/Changes to Perl 5 operators/"Note that Perl 6 is making a
consistent three-way distinction">
for more details on this distinction.

Pugs should have never admitted the first $n<2 case (by scanning
outward to a matching > and
failing+backtracking), and should instead say someting like "write $n
< 2 instead". Can you commit a
test to make sure that "1<2" is a parse failure, perhaps as t/syntax/
parsing/lt.t?

A commit bit is on your way. Remember to add yourself to AUTHORS
first. :-)

Thanks!
Audrey

0 new messages