Re: Part assignment
- To: mathgroup at smc.vnet.net
- Subject: [mg45051] Re: Part assignment
- From: Maxim <dontsendhere@.>
- Date: Sun, 14 Dec 2003 06:22:52 -0500 (EST)
- References: <brch38$2ko$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Wolf, Hartmut" wrote: > >-----Original Message----- > >From: Maxim [mailto:dontsendhere@.] To: mathgroup at smc.vnet.net > >Sent: Thursday, December 11, 2003 11:28 AM > >To: mathgroup at smc.vnet.net > >Subject: [mg45051] Part assignment > > > > > >Consider > > > >In[1]:= > >Module[ > > {L={0,0}}, > > L[[1]]=Sequence[1,1]; > > L[[2]]=2; > > L[[2]] > >] > > > >Out[1]= > >1 > > > >The first part assignment constructs a list {Sequence[1,1],0}. Then an > >interesting thing happens: part extraction functions (Part and others) > >think that the second element of this list is 1, while part assignment > >(Set) decides that the second element is 0. > > > >Maxim Rytin > >m.r at prontomail.com > > > > > > Maxim, > > obviously you like to live on the edge. Unless you complain, this is a bold > way to learn. Observe: > > In[32]:= L = {0, 0}; > L[[1]] = Sequence[1, 1]; > L[[2]] = 2; > L[[2]] > Out[35]= 1 > > In[36]:= ?L > > Global`L > > L = {Sequence[1, 1], 2} > > In[48]:= Attributes[Set] > Out[48]= {HoldFirst, Protected, SequenceHold} > > (Look up what SequenceHold means!) > > As mostly, you do have alternatives: > > In[38]:= L = {0, 0}; > L = ReplacePart[L, Unevaluated[Sequence[1, 1]], 1]; > L[[2]] = 2; > L[[2]] > > Out[41]= 2 > > In[42]:= ?L > > Global`L > > L = {1, 2, 0} > > -- > Hartmut Wolf So you think SequenceHold is the explanation? Let's see if it has any relation to my example. First assignment: L[[1]]=Sequence[1,1], or Set[Part[L,1],Sequence[1,1]]. SequenceHold simply determines that it is not converted to Set[Part[L,1],1,1]. Curiously enough, in this case it doesn't make any difference, since the latter evaluates exactly the same way. Second assignment: L[[2]]=2, or Set[Part[L,2],2]. Here L is not evaluated (because assigment treats Part in a special way -- if there were another non-holding function instead of Part, L would be evaluated), so there's simply no Sequence objects here and SequenceHold can't have any effect. So references to SequenceHold here are just fancy words -- remove the SequenceHold attribute of Set and verify that the result will be exactly the same. The evaluation rules just don't say anything explicitly about how assignments should be performed for parts of lists like {Sequence[1,1],0} (that is, after I assign a Sequence object to one of the list elements). The current approach has its logic too, but it can be misleading, for example, L[[Length@L]]=elem may result in an error. Maxim Rytin m.r at prontomail.com