Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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 (Allan Hayes)
  • Date: Fri, 13 Oct 1995 06:26:05 GMT
  • Approved:
  • Distribution: local
  • Newsgroups: wri.mathgroup
  • Organization: Wolfram Research, Inc.
  • Sender: daemon at ( )

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

There are problems with

  If[ValueQ[v1] && ValueQ[v2], {v1,v2} = {v2,v1}];

since swap evaluates its elemenents on the left hand side.

{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

  • 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