Re: Bug with Sequence
- To: mathgroup at smc.vnet.net
- Subject: [mg104619] Re: [mg104568] Bug with Sequence
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Thu, 5 Nov 2009 03:48:38 -0500 (EST)
- References: <200911040634.BAA08584@smc.vnet.net>
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 On Tue, Nov 3, 2009 at 10:34 PM, dh <dh at 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 > > > >
- References:
- Bug with Sequence
- From: dh <dh@metrohm.com>
- Bug with Sequence