interchange entries in list
- To: mathgroup at smc.vnet.net
- Subject: [mg27419] interchange entries in list
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Sun, 25 Feb 2001 00:53:46 -0500 (EST)
- Organization: University of Massachusetts, Amherst
- Sender: owner-wri-mathgroup at wolfram.com
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?