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

Bug with Sequence

46 views
Skip to first unread message

dh

unread,
Nov 4, 2009, 1:34:52 AM11/4/09
to

Hello,

has anybody an explanation for the behavior of "Sequence"? I think it is

an ugly bug.

Consider the following that shoud succesively shorten the list t:

t = {1, 2, 3}; j = 0;

While[ ++j < 4, t[[1]] = Sequence[]; Print[t]]

this returns: {2,3} three times.Dropping of the first element only seems

to work once.

If you say Information[t] you get:

t={Sequence[],2,3}

Daniel

Szabolcs Horvát

unread,
Nov 5, 2009, 4:19:50 AM11/5/09
to

Hi Daniel,

What seems to be happening when

t[[1]] = x

is evaluated is that first x is evaluated, then the first element of t
is changed to (the result of the evaluation of) x. But t itself does
not go through an evaluation process. Thus an assignment like t[[1]] is
only capable of changing the first element of t but nothing else.

If we force t to be evaluated in its entirety after every assignment,
then the program will work as expected:

While[ ++j < 4, t[[1]] = Sequence[]; t = t; Print[t]]

The fact that t[[1]] = x type assignments are not able to affect
anything else than the first element is not necessarily a bad thing.
Consider the following (admittedly unusual) example:

u = Unevaluated[{RandomInteger[], RandomInteger[]}]

If u[[1]] = 2 would trigger an evaluation of the entire contents of u,
the second element would change too. However, I wouldn't expect that to
happen in this particular example.

I hope this helps,
Szabolcs

DrMajorBob

unread,
Nov 5, 2009, 4:20:31 AM11/5/09
to
It's not a bug. How can setting t[[1]] be interpreted to ELIMINATE t[[1]]?

The same thing happens if we enter:

x = Sequence[];
x

Sequence[]

x is Set, it's not eliminated.

If you want to drop a term at each step, no problem:

t = {1, 2, 3}; j = 0;

While[++j < 4, t = Rest@t; Print[t]]

{2,3}

{3}

{}

t

{}

Or:

t = {1, 2, 3}; j = 0;

While[++j < 4, t = Drop[t, 1]; Print[t]]

{2,3}

{3}

{}

t

{}

What serious work are you trying to do, where this is a problem... even if
you DIDN'T have other ways to do what you really want?

Bobby

On Wed, 04 Nov 2009 00:34:10 -0600, dh <d...@metrohm.com> wrote:

>
>
> Hello,
>
> has anybody an explanation for the behavior of "Sequence"? I think it is
>
> an ugly bug.
>
> Consider the following that shoud succesively shorten the list t:
>
>
>
> t = {1, 2, 3}; j = 0;
>
> While[ ++j < 4, t[[1]] = Sequence[]; Print[t]]
>
>
>
> this returns: {2,3} three times.Dropping of the first element only seems
>
> to work once.
>
> If you say Information[t] you get:
>
> t={Sequence[],2,3}
>
>
>

> Daniel
>
>
>


--
DrMaj...@yahoo.com

Patrick Scheibe

unread,
Nov 5, 2009, 4:25:00 AM11/5/09
to
Hi,

no it's not a bug. It's called SequenceHold and is an Attribute which
prevents the stuff from flattening out.

t = {1, 2, 3}; j = 0;

While[++j < 4, t[[1]] = Sequence[]; t = Flatten[t]; Print[t]]

Cheers
Patrick

Bob Hanlon

unread,
Nov 5, 2009, 4:25:11 AM11/5/09
to

As stated in the documentation for Sequence:

"Assignment and replacement functions have attribute SequenceHold."

t = {1, 2, 3}; j = 0;

While[++j < 4, t[[1]] = Sequence[]; Print[t = ReleaseHold[t]]]

{2,3}

{3}

{}


Bob Hanlon

---- dh <d...@metrohm.com> wrote:

=============

Leonid Shifrin

unread,
Nov 5, 2009, 4:25:34 AM11/5/09
to
Hi Daniel,

This does not seem like a bug to me. Let us see how the list is stored after
the first assignment:

In[1] =
t={1, 2, 3}; j = 0;
t[[1]]=Sequence[];

?t
Global`t
t={Sequence[],2,3}

We see that the first element is still there (you have actually noticed
that!). We don't really change the size of a list (as stored in OwnValues
etc) by assigning Sequence[] to some of its elements - Sequence[] is just as
good an element as anything else. All the Sequence[] magic happens at
evaluation stage - the size of an *evaluated* list becomes *effectively*
smaller as a result of evaluation (during which Sequence-s inside heads
without SequenceHold attribute are spliced).

This seems entirely consistent to me, given that lists are internally
implemented as arrays and then to really change the size of a list as it is
stored, we would generally need O(n) operations where n is the size of the
list. OTOH, operations like Part and asignments should not be concerned with
anything else than just assigning parts of expressions, and such list
rearrangements should be beyond their scope IMO.

Returning to your example, all subsequent assignments simply re-assign
Sequence[] to the first element, and don't touch the rest - thus the result
that puzzled you.

The following two modifications achieve what you presumably wanted:

In[2] =
t={1, 2, 3}; j = 0;
While[++j<4,t=(t[[1]]=Sequence[];t);Print[t]]

{2,3}
{3}
{}

?t
Global`t
t={}


In[3] =


t = {1, 2, 3}; j = 0;

While[++j < 4, t[[j]] = Sequence[]; Print[t]]

{2,3}

{3}

{}

?t

Global`t

t={Sequence[],Sequence[],Sequence[]}

Note again that the final state of the list <t> is different in each case -
it is an empty list in the first method and a list of 3 Sequence[] elements
in the second. However, should you just use the <t>, in most cases you won't
see the difference.

Hope this helps.

Regards,
Leonid

Kurt TeKolste

unread,
Nov 5, 2009, 4:25:45 AM11/5/09
to
I assume that you will get lots of answers on this but:

RInforeplacing the first element of t with Sequence[] multiple times is
the same as doing it once. Try replacing t[[j]].

ekt

t = {1, 2, 3}; j = 0;

While[++j < 4, t[[j]] = Sequence[]; Print[t]]

{2,3}

{3}

{}
Information[t]

Global`t

t={Sequence[],Sequence[],Sequence[]}

On Wed, 04 Nov 2009 01:34 -0500, "dh" <d...@metrohm.com> wrote:
>
>
> Hello,
>
> has anybody an explanation for the behavior of "Sequence"? I think it is
>
> an ugly bug.
>
> Consider the following that shoud succesively shorten the list t:
>
>
>
> t = {1, 2, 3}; j = 0;
>
> While[ ++j < 4, t[[1]] = Sequence[]; Print[t]]
>
>
>
> this returns: {2,3} three times.Dropping of the first element only seems
>
> to work once.
>
> If you say Information[t] you get:
>
> t={Sequence[],2,3}
>
>
>
> Daniel
>
>
>

Regards,
Kurt Tekolste


Sseziwa Mukasa

unread,
Nov 5, 2009, 4:26:08 AM11/5/09
to

On Nov 4, 2009, at 1:34 AM, dh wrote:

>
>
> Hello,
>
> has anybody an explanation for the behavior of "Sequence"? I think
> it is
>
> an ugly bug.
>
> Consider the following that shoud succesively shorten the list t:
>
>
>
> t = {1, 2, 3}; j = 0;
>
> While[ ++j < 4, t[[1]] = Sequence[]; Print[t]]
>

You are only assigning t[[1]] at each iteration, t itself remains of
length 3.

> this returns: {2,3} three times.Dropping of the first element only
> seems
>
> to work once.
>
> If you say Information[t] you get:
>
> t={Sequence[],2,3}

Which is what happens when you assign Sequence[] to the first element
of t. If you want to remove elements from t use t = Rest[t].

Regards,

Ssezi


DrMajorBob

unread,
Nov 6, 2009, 5:12:44 AM11/6/09
to
On the other hand, "forcing the evaluation" of x below does nothing:

x = Sequence[]
x = x

Sequence[]

Sequence[]

and yet:

x = Sequence[]
x = {x}

Sequence[]

{}

Bobby

On Thu, 05 Nov 2009 02:51:19 -0600, Szabolcs Horv�t <szho...@gmail.com>
wrote:

> On 2009.11.04. 7:34, dh wrote:

> Hi Daniel,
>
> What seems to be happening when
>
> t[[1]] = x
>

> is evaluated is that first x is evaluated, then the first element of t


> is changed to (the result of the evaluation of) x. But t itself does
> not go through an evaluation process. Thus an assignment like t[[1]] is
> only capable of changing the first element of t but nothing else.
>
> If we force t to be evaluated in its entirety after every assignment,
> then the program will work as expected:
>
> While[ ++j < 4, t[[1]] = Sequence[]; t = t; Print[t]]
>
> The fact that t[[1]] = x type assignments are not able to affect
> anything else than the first element is not necessarily a bad thing.
> Consider the following (admittedly unusual) example:
>
> u = Unevaluated[{RandomInteger[], RandomInteger[]}]
>
> If u[[1]] = 2 would trigger an evaluation of the entire contents of u,
> the second element would change too. However, I wouldn't expect that to
> happen in this particular example.
>
> I hope this helps,
> Szabolcs
>


--
DrMaj...@yahoo.com

0 new messages