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

Owner: pmichaud <pmichaud [at] pobox.com>
Requestors: TimTom <tim.bollman [at] live.com>
Cc:
AdminCc:

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



Subject: Complex Recursive Sequences
Date: Sun, 23 Dec 2012 16:28:02 -0800
To: <rakudobug [...] perl.org>
From: Timothy Bollman <tim.bollman [...] live.com>
Download (untitled) / with headers
text/plain 1.2k
Rakudo Version: 2012.12-38-gc8de2eb


Story:


I was attempting to write a different solution to the Collatz Sequence problem in this years Advent Calendar.  I figured it was a sequence of numbers, the sequence operation should be able to rock this problem efficiently.  I'm not sure if this is supposed to be supported by the implementation, but if it is, I believe the problem comes down to Rakudo not liking to vivify later elements in the sequence in order to vivify the current element.


Works:
my @collatz;
@collatz := Inf, 1, (2 .. *).map: -> $n {@collatz[$n - 1] + 1}
say @collatz[0 .. 5]
>Inf 1 2 3 4 5


Doesn't Work:
my @collatz;
@collatz := Inf, 1, (2 .. *).map -> $n { $n %% 2 ?? @collatz[$n div 2] !! @colatz[3 * $n + 1] }
say @collatz[0 .. 5]
splice() not implemented in class 'Mu'
in method reify at src/gen/CORE.setting:5409
in method reify at src/gen/CORE.setting:5407
in method gimme at src/gen/CORE.setting:5797
in method exists at src/gen/CORE.setting:5786
in method at_pos at src/gen/CORE.setting:5766
in method postcircumfix:<[ ]> at src/gen/CORE.setting:1337
in method postcircumfix:<[ ]> at src/gen/CORE.setting:1384
in method postcircumfix:<[ ]> at src/gen/CORE.setting:5042


-Tim
irc: Tim-Tom

Download (untitled) / with headers
text/plain 746b
It isn't so much computing later values in the sense of higher numbered ones. It's computing ones that haven't been computed yet. The first example doesn't work either, if you don't access the values sequentially: Show quoted text
> my @collatz := Inf, 1, (2 .. *).map: -> $n {@collatz[$n - 1] + 1} ;1
1 Show quoted text
> @collatz[5]
use of uninitialized value of type Nil in numeric contextuse of uninitialized... (On my system the error string is in an endless loop) When you accessed it with ^5, you allowed each element to be computed before it was needed by others. It doesn't have to be linear: Show quoted text
> my @collatz := Inf, 1, (2 .. *).map: -> $n {@collatz[$n div 2] } ;1
1 Show quoted text
> @collatz[0]
Inf Show quoted text
> @collatz[1]
1 Show quoted text
> @collatz[2]
1 Show quoted text
> @collatz[4]
1 Show quoted text
> @collatz[8]
1 Show quoted text
> @collatz[16]
1
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 1.7k
On Sun Dec 23 16:28:31 2012, TimTom wrote: Show quoted text
> > Rakudo Version: 2012.12-38-gc8de2eb > > > Story: > > > I was attempting to write a different solution to the Collatz Sequence > problem in this years Advent Calendar. I figured it was a sequence of > numbers, the sequence operation should be able to rock this problem > efficiently. I'm not sure if this is supposed to be supported by the > implementation, but if it is, I believe the problem comes down to > Rakudo not liking to vivify later elements in the sequence in order to > vivify the current element. > > > Works: > my @collatz; > @collatz := Inf, 1, (2 .. *).map: -> $n {@collatz[$n - 1] + 1} > say @collatz[0 .. 5]
> > Inf 1 2 3 4 5
> > > Doesn't Work: > my @collatz; > @collatz := Inf, 1, (2 .. *).map -> $n { $n %% 2 ?? @collatz[$n div 2] > !! @colatz[3 * $n + 1] } > say @collatz[0 .. 5] > splice() not implemented in class 'Mu' > in method reify at src/gen/CORE.setting:5409 > in method reify at src/gen/CORE.setting:5407 > in method gimme at src/gen/CORE.setting:5797 > in method exists at src/gen/CORE.setting:5786 > in method at_pos at src/gen/CORE.setting:5766 > in method postcircumfix:<[ ]> at src/gen/CORE.setting:1337 > in method postcircumfix:<[ ]> at src/gen/CORE.setting:1384 > in method postcircumfix:<[ ]> at src/gen/CORE.setting:5042 > > > -Tim > irc: Tim-Tom >
Neither of these work now (2016.07.1) The first outputs: (Inf 1 (...) Nil Nil Nil) And the second ===SORRY!=== Error while compiling /Users/coke/p6 Unexpected block in infix position (missing statement control word before the expression?) at /Users/coke/p6:2 ------> @collatz := Inf, 1, (2 .. *).map⏏ -> $n { $n %% 2 ?? @collatz[$n div 2] ! expecting any of: infix infix stopper -- Will "Coke" Coleda
Download (untitled) / with headers
text/plain 868b
The first one still works in current Rakudo if you add a | to slip the `map` sequence into the parent list. Here it is in slightly simplified form: ➜ my @collatz = Inf, 1, |(2 .. *).map: { @collatz[$_ - 1] + 1 }; say @collatz[0 .. 5]; (Inf 1 2 3 4 5) As for the second one, after giving it the same treatment (and fixing its two syntax typos), it simply hangs indefinitely after the third term: ➜ my @collatz = Inf, 1, |(2 .. *).map: { $_ %% 2 ?? @collatz[$_ div 2] !! @collatz[3 * $_ + 1] }; say @collatz[$_] for 0 .. 5; Inf 1 1 So it seems that Timothy Bollman's key observation in the original ticket is still correct: Rakudo won't reify a later term of a sequence, in order to reify an earlier term. I have no idea if it *should* work (ask TimToady), or if it would even be feasible given how lists/sequences work (ask jnthn).
Download (untitled) / with headers
text/plain 431b
Here is a simpler test-case demonstrating the issue (i.e. recursive out-of-order reification not working): ➜ my @a = (0..*).map({ $_ == 0 ?? 10 !! 5 }); say @a[^5]; (10 5 5 5 5) ➜ my @a = (0..*).map({ $_ == 0 ?? 2 * @a[1] !! 5 }); say @a[^5]; # This one generates junk results (5 5 10 5 5) ➜ my @a = (0..*).map({ $_ %% 2 ?? 2 * @a[$_ + 1] !! 5 }); say @a[^5]; # This one hangs


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