Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

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

Search the Archive

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?


  • 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