MathGroup Archive 2001

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

Search the Archive

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

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