Re: Q: efficient in-place list element replacement?
- To: mathgroup at smc.vnet.net
- Subject: [mg21140] Re: Q: efficient in-place list element replacement?
- From: Bojan Bistrovic <bojanb at physics.odu.edu>
- Date: Fri, 17 Dec 1999 01:21:41 -0500 (EST)
- Organization: Old Dominion Universityaruba
- References: <83208o$gik@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
This might shed more light on your problem if not solve it. After typing in your example, try the following: In[6]:= b=Unevaluated[a] Out[6]= {4,5,a,b,b,7,67,89} In[7]:= c=a Out[7]= {4,5,a,b,b,7,67,89} The output of "a", "b" and "c" will be the same, but they aren't stored in memory in the same way. This is how they are stored internaly: In[8]:= Definition[a] Out[8]= {4,5,Sequence["a","b","c"],7,67,89} In[9]:= Definition[b] Out[9]= a In[10]:= Definition[c] Out[10]= {4,5,a,b,b,7,67,89} Therefore, doing something like a[[7]]=Pi doesn't work because Set has attribute HoldFirst which prevents the left hand side of the equation from being evaluated. You might think that something like Unprotect[Set]; ClearAttributes[Set, HoldFirst] would help, but it won't (I've tried); it will start producing weird error messages. Functions like Part, Length or FullForm evaluate their arguments first so you don't see the Sequence fuction. On the other hand, commands like Unevaluated[a] or Hold[a] will prevent the List {4,5,...} from being assigned to the symbol "a" so the result will be just Unevaluated[a] or Hold[a] Last, but not least, there's a "trick" that will solve your problem (altho it probably involves copying). Try following: In[11]:= a=a Out[11]= {4,5,a,b,b,7,67,89} In[12]:= Definition[a] Out[12]= {4,5,a,b,b,7,67,89} What happened is this: Set evaluates the right-hand side of the aquation, but not the left-hand; so the right-hand "a" was evaluated, replaced by {4,5,a,b,b,7,67,89} and then assigned to the unevaluated symbol "a" on the left-hand side.