Push and Pop on Infinite lists

4 views
Skip to first unread message

Stevan Little

unread,
Apr 17, 2005, 11:33:45 PM4/17/05
to perl6-c...@perl.org
I am working on edge cases and error cases for some of the t/builtin/
tests.

I know its a silly thing to do, but how should push and pop behave with
(0 .. Inf) lists?

I read through this thread:
http://www.mail-archive.com/perl...@perl.org/msg39891.html

But I did not see an "answer", only various suggestions.

Currently in Pugs these two examples:

pugs -e 'my @p = (0 .. Inf); push @p, 10;'
pugs -e 'my @p = (0 .. Inf); pop(@p);'

will go on forever (as you would expect it to do).

Is this the correct behavior? Or should it return Inf or somesuch? Or
even maybe error?

Thanks,

Stevan

Larry Wall

unread,
Apr 18, 2005, 10:40:08 AM4/18/05
to perl6-c...@perl.org
On Sun, Apr 17, 2005 at 11:33:45PM -0400, Stevan Little wrote:
: I am working on edge cases and error cases for some of the t/builtin/
: tests.
:
: I know its a silly thing to do, but how should push and pop behave with
: (0 .. Inf) lists?
:
: I read through this thread:
: http://www.mail-archive.com/perl...@perl.org/msg39891.html
:
: But I did not see an "answer", only various suggestions.
:
: Currently in Pugs these two examples:
:
: pugs -e 'my @p = (0 .. Inf); push @p, 10;'
: pugs -e 'my @p = (0 .. Inf); pop(@p);'
:
: will go on forever (as you would expect it to do).

Well, running forever could be construed as somewhat antisocial.

: Is this the correct behavior? Or should it return Inf or somesuch? Or
: even maybe error?

An infinite list consists of two parts, the part we've generated, and
the part we haven't yet generated. The first part acts just like an
ordinary Perl 5 array and can be shifted, unshifted, or subscripted.
If your shift or your subscript asks for something beyond the end of
the generated section, then you have to look at your generator section
to derive more generated values.

However, push and pop are special cases, since they're not changing
the front of the array, but they're not subscripting operations
either, exactly. The generator section can presumably contain one
or more generators. Those generators can each be set up to produce
either finite or infinite lists. So it is meaningful to push 10
on the end--it adds a generator that returns 10..10 to the list of
generators, after which @p[-1] could reasonably return 10, as could pop.

Likewise you could push another 0..Inf. If the code is smart enough
to return 10 if the final generator is 10..10, then it's probably also
smart enough to return Inf for 0..Inf. The difference is that if
you pop 10..10, it decrements it to 10..9, then notices it's a null
generator and removes it. If you pop 0..Inf, it pops the final Inf,
and decrements the 0..Inf to, er, 0..Inf.

I think that's a reasonable set of semantics, and perhaps even vaguely
useful. On the other hand, till someone implements it, it would
be okay to fail("Not implemented").

BTW, A6 claims the method to get from an array object to its underlying
Lazy generator list is the .specs method. As far as I know that's
still the case.

Larry

Reply all
Reply to author
Forward
0 new messages