MathGroup Archive 1995

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

Search the Archive

Re: A simple swap function

  • Subject: [mg2165] Re: A simple swap function
  • From: ianc (Ian Collier)
  • Date: Wed, 11 Oct 1995 05:58:41 GMT
  • Approved: usenet@wri.com
  • Distribution: local
  • Newsgroups: wri.mathgroup
  • Organization: Wolfram Research, Inc.
  • Sender: daemon at wri.com ( )

In article <44qj8g$s22 at ralph.vnet.net>, valentin at wam.umd.edu (William F.
Campbell) wrote:

>   I think I must be missing something straightforward.  I want a function
> to swap the values of 2 lists.  I defined ab={a,b}, and ba={b,a}, and
> swap[v1_,v2_]:=Module[{temp=v1},v1=v2;v2=temp;], and I get an Iteration
> limit exceeded error.  When I look at ab, it looks like {Hold[a],Hold[b]}
> (or maybe {Hold[b],Hold[a], I don't remember), and ba looks opposite.  When
> I tried to use the same function on numbers, it didn't work either.  What's
> wrong with what I'm doing, and how can I do what I want to do?


Well, in this example you can simply use the Reverse function to
swap your values:

In[9]:=
    ab = {a,b}
Out[9]=
    {a, b}
In[10]:=
    ba = Reverse[ ab ]
Out[10]=
    {b, a}

I hope this helps.

--Ian

-----------------------------------------------------------
Ian Collier
Technical Sales Support
Wolfram Research, Inc.
-----------------------------------------------------------
tel:(217)-398-0700     fax:(217)-398-0747      ianc at wri.com
Wolfram Research Home Page:             http://www.wri.com/
-----------------------------------------------------------


  • Prev by Date: Q: ROTATING ANIMATIONS in MMA?
  • Next by Date: Differential operators
  • Previous by thread: Re: A simple swap function
  • Next by thread: Re: A simple swap function