MathGroup Archive 2001

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

Search the Archive

interchange entries in list

  • To: mathgroup at
  • Subject: [mg27419] interchange entries in list
  • From: Murray Eisenberg <murray at>
  • Date: Sun, 25 Feb 2001 00:53:46 -0500 (EST)
  • Organization: University of Massachusetts, Amherst
  • Sender: owner-wri-mathgroup at

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}]]; 

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?

  • Prev by Date: Hydrodybnamics of liquid and gas
  • Next by Date: Editing formulas for the web
  • Previous by thread: Hydrodybnamics of liquid and gas
  • Next by thread: Re: interchange entries in list