Re: List manipulation question
- To: mathgroup at smc.vnet.net
- Subject: [mg15944] Re: List manipulation question
- From: Eckhard Hennig <hennig at itwm.uni-kl.de>
- Date: Wed, 17 Feb 1999 23:34:07 -0500
- Organization: ITWM
- References: <7a2b7v$1t6@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Maarten.vanderBurgt at icos.be schrieb in Nachricht
<7a2b7v$1t6 at smc.vnet.net>...
>
>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]?
[...snip...]
Dear Marten,
you get error message because Mathematica replaces the parameter L in your
function by the value of mylist. This causes Swap23 to make an invalid
assignment of the form
{1, 2, 3}[[2]] = {1, 2, 3}[[3]]
To prevent Mathematica from evaluating L prematurely, use the attribute
HoldAll:
SetAttributes[Swap23, HoldAll]
Swap23[L_]:=Module[
{temp},
temp = L[[2]];
L[[2]]=L[[3]];
L[[3]] =temp;
L
]
HTH,
Eckhard
-----------------------------------------------------------
Dipl.-Ing. Eckhard Hennig mailto:hennig at itwm.uni-kl.de
Institut fuer Techno- und Wirtschaftsmathematik e.V. (ITWM)
Erwin-Schroedinger-Strasse, 67663 Kaiserslautern, Germany
Voice: +49-(0)631-205-3126 Fax: +49-(0)631-205-4139
http://www.itwm.uni-kl.de/as/employees/hennig.html
ITWM - Makers of Analog Insydes for Mathematica
http://www.itwm.uni-kl.de/as/products/ai
-----------------------------------------------------------