Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Perl 6 try to evaluate an infinite list only in one of two similar situations?

Tags:

raku

Suppose I define a lazy, infinite array using a triangular reduction at the REPL, with a single element pasted onto the front:

> my @s = 0, |[\+] (1, 2 ... *)
[...]

I can print out the first few elements:

> @s[^10]
(0 1 3 6 10 15 21 28 36 45)

I'd like to move the zero element inside the reduction like so:

> my @s = [\+] (0, |(1, 2 ... *))

However, in response to this, the REPL hangs, presumably by trying to evaluate the infinite list.

If I do it in separate steps, it works:

> my @s = 0, |(1, 2 ... *)
[...]
> ([\+] @s)[^10]
(0 1 3 6 10 15 21 28 36 45)

Why doesn't the way that doesn't work...work?

like image 487
Sean Avatar asked Jul 23 '18 18:07

Sean


1 Answers

Short answer:

It is probably a bug.


Long answer:

(1, 2 ... *) produces a lazy sequence because it is obviously infinite, but somehow that is not making the resulting sequence from being marked as lazy.

Putting a sequence into an array @s causes it to be eagerly evaluated unless it is marked as being lazy.


Quick fix:

Append lazy to the front.

> my @s = [\+] lazy 0, |(1, 2 ... *)
[...]
> @s[^10]
(0 1 3 6 10 15 21 28 36 45)
like image 147
Brad Gilbert Avatar answered Oct 23 '22 04:10

Brad Gilbert