Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: List manipulation question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15918] Re: [mg15852] List manipulation question
  • From: Jurgen Tischer <jtischer at col2.telecom.com.co>
  • Date: Wed, 17 Feb 1999 23:33:53 -0500
  • Organization: Universidad del Valle
  • References: <199902122340.SAA02320@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Maarten,
in the evaluation of your function, first mylist is replaced by {1,2,3}
and this is given to the Module. So you have a variable by value and not
by address and Mathematica complains. The solution depends on what you
really want to achieve. If you want your function to return the
permutated list without changing the argument list, you define a local
list inside the Module:

Swap23[L_List]:=Module
[
          {temp,Li=L},
           temp = Li[[2]];
          Li[[2]]=Li[[3]];
          Li[[3]] =temp;
          Li
]

If you want the argument changed, you can do the following:

Swap23[L_Symbol]:=Module
[
          {temp},
           temp = L[[2]];
          L[[2]]=L[[3]];
          L[[3]] =temp;
          L
]

SetAttributes[Swap23,HoldAll]

Jurgen

Maarten.vanderBurgt at icos.be wrote:
> 
> 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}




  • Prev by Date: Finite Differences and Mathematica?
  • Next by Date: Re: Fourier and FFT: Powers of 2 only?
  • Previous by thread: List manipulation question
  • Next by thread: RE: List manipulation question