MathGroup Archive 2001

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

Search the Archive

RE: interchange entries in list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27470] RE: [mg27419] interchange entries in list
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
  • Date: Tue, 27 Feb 2001 00:37:22 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

for my answer see below:

-----Original Message-----
From: Murray Eisenberg [mailto:murray at math.umass.edu]
To: mathgroup at smc.vnet.net
Subject: [mg27470] [mg27419] interchange entries in list


Is there any hidden trouble with the following function for
interchanging two entries in a list?

  swap[v_List, i_, j_] := v /. {v[[i]]->v[[j]], v[[j]]->v[[i]]}

It seems to work, yet I'm uncomfortable.

Note that I am _not_ uncomfortable with a construction such as

  vec[[{1,2}]] = v[[{2,1}]]

or the Module

  swapItems[v_List, i_, j_] := 
     Module[ {t = v},
       t[[{i, j}]] = t[[{j, i}]]; 
       t
     ]

that expresses this idea.

With "swap", evidently my concern is with when what items get replaced
by what.  In other words, why SHOULD "swap"  work?

-------------------

Well, Murray, it doesn't! Having looked at the Trace

In[20]:= Trace[swap[Range[5], 2, 5]]

you might try

In[21]:= swap[{1, 2, 3, 2, 1}, 2, 3]
Out[21]= {1, 3, 2, 3, 1}

which is not what swap should do.

-- Hartmut 



  • Prev by Date: Re: DumpSave
  • Next by Date: Re: What is the difference equation of a Butterworth filter?
  • Previous by thread: Re: interchange entries in list
  • Next by thread: Editing formulas for the web