Re: List manipulation question
- To: mathgroup at smc.vnet.net
- Subject: [mg15984] Re: List manipulation question
- From: Hartmut Wolf <hw at gsmail01.darmstadt.dsh.de>
- Date: Fri, 19 Feb 1999 03:27:04 -0500
- Organization: debis Systemhaus
- References: <7a2b7v$1t6@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Dear Marten, Maarten.vanderBurgt at icos.be schrieb: > > Dear all, > > In the following piece of code I define a function Swap23 which is ment > to swap elements 2 and 3 in a list. > Executing the function on a simple list I get an error. Why do I get > this error? Why do I not get this error when I execute the commnad from > Swap23 "by hand" as is shown in In[4]? > > Thanks for any help > > Maarten van der Burgt > Icos Vision Systems > Leuven > Belgium > > In[1]:=Swap23[L_List]:=Module > [ > {temp}, > temp = L[[2]]; > L[[2]]=L[[3]]; > L[[3]] =temp; > L > ] > > In[2]:= mylist = {1,2,3}; > > In[3]:= Swap23[mylist] > > Set::"setps": "\!\({1, 2, 3}\) in assignment of part is not a symbol." > Set::"setps": "\!\({1, 2, 3}\) in assignment of part is not a symbol." > > Out[3]= {1,2,3} > > In[4]:= temp = mylist[[2]]; > mylist[[2]]=mylist[[3]]; > mylist[[3]] =temp; > mylist > > Out[4]= {1,3,2} What you *really* did was the following: In[6]:=mylist = {1,2,3}; In[7]:=temp = {1,2,3}[[2]] Out[7]=2 In[8]:={1,2,3}[[2]] = {1,2,3}[[3]] Set::"setps": "\!\({1, 2, 3}\) in assignment of part is not a symbol." Out[8]=3 In[9]:={1,2,3}[[3]] =temp Set::"setps": "\!\({1, 2, 3}\) in assignment of part is not a symbol." Out[9]=2 In[10]:={1,2,3} Out[10]={1,2,3} To understand it define In[11]:=Swap23[L_]:=Module [ {temp}, temp = L[[2]]; L[[2]]=L[[3]]; L[[3]] =temp; L ] In[12]:=mylist = {1,2,3}; In[13]:=Swap23[Unevaluated[mylist]] Out[13]={1,3,2} No error! (you can't use Unevaluated if you constrain the argument to a List!) your's Hartmut Wolf, debis Systemhaus, Darmstadt, Germany =======[mailto:hwolf at debis.com]=======