       Re: Re: A simple swap function

• To: mathgroup at smc.vnet.net
• Subject: [mg2199] Re: [mg2151] Re: [mg2132] A simple swap function
• From: Allan Hayes <hay at haystack.demon.co.uk>
• Date: Fri, 13 Oct 1995 02:26:05 -0400

```In [mg2151] Re: [mg2132] A simple swap function
Richard Mercer sugggested a  function for swapping the values of
two symbols.

Richard:
There are problems with

swap[v1_Symbol,v2_Symbol]:=
If[ValueQ[v1] && ValueQ[v2], {v1,v2} = {v2,v1}];

since swap evaluates its elemenents on the left hand side.

Examples:
{a = 1; b = 2; swap[a,b],  {a,b}}
{swap[1, 2], {1, 2}}
{a = va; b = vb; swap[a,b], {a,b}}
{Null, {va, vb}}

Solution: give swap the attribute HoldAll

SetAttributes[swap, HoldAll]

{a = 1; b = 2; swap[a,b],  {a,b}}
{{2, 1}, {2, 1}}
{a = va; b = vb; swap[a,b], {a,b}}
{{vb, va}, {vb, va}}

Allan Hayes
hay at haystack.demon.co.uk

```

• Prev by Date: Re: MMA movie with sound to QT
• Next by Date: Mathematica Training Courses
• Previous by thread: Re: Re: A simple swap function
• Next by thread: Re: A simple swap function