MathGroup Archive 1995

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

Search the Archive

Re: A simple swap function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg2145] Re: A simple swap function
  • From: rmarimon at leland.stanford.edu (Ricardo Marimon)
  • Date: Wed, 4 Oct 1995 01:58:34 -0400
  • Organization: Stanford University

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?

If I remember correctly, the parameters in Mathematica are passed by value
only.  Any attempt to change the value of a parameter will not work, since
you would be changing the value of the paramater and not the value of the
variable you passes to the function...

You could try writting

{v1, v2} = {v2, v1}

this will do the swap you intend and is very simple...

- Ricardo Marimon
  rmarimon at leland.stanford.edu


  • Prev by Date: Long filenames
  • Next by Date: RE: Re: MMA for finance/econometrics
  • Previous by thread: Re: Help on Polynomial Convolution
  • Next by thread: A simple swap function