MathGroup Archive 1995

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

Search the Archive

Re: Re: A simple swap function

  • Subject: [mg2199] Re: [mg2151] Re: [mg2132] A simple swap function
  • From: hay at haystack.demon.co.uk (Allan Hayes)
  • Date: Fri, 13 Oct 1995 06:26:05 GMT
  • Approved: usenet@wri.com
  • Distribution: local
  • Newsgroups: wri.mathgroup
  • Organization: Wolfram Research, Inc.
  • Sender: daemon at wri.com ( )

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: Mathematica & Security Software
  • Next by Date: Mathematica Training Courses
  • Previous by thread: Re: A simple swap function
  • Next by thread: Re: Re: A simple swap function