MathGroup Archive 2001

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

Search the Archive

Re: interchange entries in list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27455] Re: interchange entries in list
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sun, 25 Feb 2001 20:56:03 -0500 (EST)
  • References: <97a7nt$alt@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Murray,
One problem,

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


swap[{a,b,a},2,1]

        {b,a,b}

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Murray Eisenberg" <murray at math.umass.edu> wrote in message
news:97a7nt$alt at smc.vnet.net...
> 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: Re: Plot[(1 + 10^(-k))^(10^k), {k, 2, 9}]
  • Next by Date: multiplelistplot,listplot
  • Previous by thread: interchange entries in list
  • Next by thread: RE: interchange entries in list