Skip Menu |
Report information
Id: 77474
Status: open
Priority: 0/
Queue: perl6

Owner: pmichaud <pmichaud [at] pobox.com>
Requestors: masak <cmasak [at] gmail.com>
Cc:
AdminCc:

Severity: (no value)
Tag: Bug
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Subject: [BUG] Something strange happens when doing binding in a subsignature as part of the Hamming problem in Rakudo
Date: Fri, 27 Aug 2010 23:15:40 +0200
To: rakudobug [...] perl.org
From: Carl Mäsak <cmasak [...] gmail.com>
Download (untitled) / with headers
text/plain 1.7k
<TimToady> I think I've found a binding bug. http://wall.org/~larry/hamming.p6 #!/usr/local/bin/perl6 #hamming = 1 : map (2*) hamming `merge` map (3*) hamming `merge` map (5*) hamming # where merge (x:xs) (y:ys) # | x < y = x : xs `merge` (y:ys) # | x > y = y : (x:xs) `merge` ys # | otherwise = x : xs `merge` ys sub infix:<merge> (@x [$x, *@xtail], @y [$y,*@ytail]) { if $x < $y { $x, (@xtail merge @y) } elsif $x > $y { $y, (@x merge @ytail) } else { $x, (@xtail merge @ytail) } } my @hamming := (1, (@hamming X* 2) merge (@hamming X* 3) merge (@hamming X* 5)); say ~@hamming[^20]; <masak> ooh, the Hamming sequence! <TimToady> I would guess that the *@xtail is not successful in binding to the conjectural part of the @hamming array <masak> TimToady: any reason why it shouldn't bind? <TimToady> rakudo: sub foo (@x [$xhead, *@xtail]) { say ~@xtail }; foo([1,2,3]) [21:33] <p6eval> rakudo 18189a: OUTPUT«2 3␤» <masak> \o/ <Tene> perl6: my @x = 1..5; sub foo(@a [$a, *@atail]) { say "head: $a\ntail: {@atail.perl}"; }; foo(@x); <p6eval> rakudo 18189a: OUTPUT«head: 1␤tail: [2, 3, 4, 5]␤» <TimToady> rakudo: sub foo (@x [$xhead, *@xtail]) { say ~@xtail }; foo([1, (2...3)]) <p6eval> rakudo 18189a: OUTPUT«2 3␤» <TimToady> rakudo: sub foo (@x [$xhead, *@xtail]) { say ~@xtail[^3] }; foo([1, 'x' xx *]) <p6eval> rakudo 18189a: OUTPUT«(timeout)» <TimToady> anyway, hamming is sort of an acid test for laziness, the way man-or-boy is for scoping <TimToady> and currently we flunk... <pmichaud> file a bug report for hamming, please. I know enough of the binding codenow that I think I might be able to fix it up. <pmichaud> I'd like to see that one work. * masak submits rakudobug for hamming
Download (untitled) / with headers
text/plain 640b
The code as-is will never work, because at some point it'll pass an empty list to one side of the 'merge' operator, in which case the subsignature binding fails. Here is an updated version (see also https://gist.github.com/1704555 ) multi sub infix:<merge> (@ [], @y) is default { @y }; multi sub infix:<merge> (@x, @ []) { @x }; multi sub infix:<merge> (@x [$x, *@xtail], @y [$y,*@ytail]) { if $x < $y { $x, (@xtail merge @y) } elsif $x > $y { $y, (@x merge @ytail) } else { $x, (@xtail merge @ytail) } } my @hamming := (1, (@hamming X* 2) merge (@hamming X* 3) merge (@hamming X* 5)); say ~@hamming[^20]; output on current rakudo: 1
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 1.2k
On Mon Jan 30 06:13:36 2012, moritz wrote: Show quoted text
> The code as-is will never work, because at some point it'll pass an > empty list to one side of the 'merge' operator, in which case the > subsignature binding fails. > > Here is an updated version (see also https://gist.github.com/1704555 ) > > multi sub infix:<merge> (@ [], @y) is default { @y }; > multi sub infix:<merge> (@x, @ []) { @x }; > multi sub infix:<merge> (@x [$x, *@xtail], @y [$y,*@ytail]) { > if $x < $y { $x, (@xtail merge @y) } > elsif $x > $y { $y, (@x merge @ytail) } > else { $x, (@xtail merge @ytail) } > } > > my @hamming := (1, (@hamming X* 2) merge (@hamming X* 3) merge (@hamming > X* 5)); > > say ~@hamming[^20]; > > output on current rakudo: 1
11:17 < [Coke]> moritz, TimToady: what is the expected output of the last bit of code on https://rt.perl.org/Ticket/Display.html?id=77474 ? 11:18 < [Coke]> FYI, the current output is 1 0 11:19 < TimToady> http://rosettacode.org/wiki/Hamming_numbers 11:20 < TimToady> but we need the list refactor before that has a hope of working 11:20 < [Coke]> so, "1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36" is the expected output? 11:21 < TimToady> yes, if Perl 6 were as good as Haskell at recursive definitions... -- Will "Coke" Coleda
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 1.4k
On Tue Sep 02 08:21:59 2014, coke wrote: Show quoted text
> On Mon Jan 30 06:13:36 2012, moritz wrote:
> > The code as-is will never work, because at some point it'll pass an > > empty list to one side of the 'merge' operator, in which case the > > subsignature binding fails. > > > > Here is an updated version (see also https://gist.github.com/1704555 > > ) > > > > multi sub infix:<merge> (@ [], @y) is default { @y }; > > multi sub infix:<merge> (@x, @ []) { @x }; > > multi sub infix:<merge> (@x [$x, *@xtail], @y [$y,*@ytail]) { > > if $x < $y { $x, (@xtail merge @y) } > > elsif $x > $y { $y, (@x merge @ytail) } > > else { $x, (@xtail merge @ytail) } > > } > > > > my @hamming := (1, (@hamming X* 2) merge (@hamming X* 3) merge > > (@hamming > > X* 5)); > > > > say ~@hamming[^20]; > > > > output on current rakudo: 1
> > 11:17 < [Coke]> moritz, TimToady: what is the expected output of the > last bit > of code on > https://rt.perl.org/Ticket/Display.html?id=77474 ? > 11:18 < [Coke]> FYI, the current output is 1 0 > 11:19 < TimToady> http://rosettacode.org/wiki/Hamming_numbers > 11:20 < TimToady> but we need the list refactor before that has a hope > of > working > 11:20 < [Coke]> so, "1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 > 36" is > the expected output? > 11:21 < TimToady> yes, if Perl 6 were as good as Haskell at recursive > definitions...
Both GLR & NOM now complain that you can't use @hamming to declare @hamming. -- Will "Coke" Coleda


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org