MathGroup Archive 2010

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

Search the Archive

changing variables in place

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113010] changing variables in place
  • From: Sebastian Schmitt <sschmitt at physi.uni-heidelberg.de>
  • Date: Mon, 11 Oct 2010 05:14:53 -0400 (EDT)

Dear all!

I'm new to Mathematica with a background mostly in C++. Many times I
have the impression that my style is not natural-Mathematica
(Mathematicaesque so to say).

Consider for example the case where I want to change the value of a
variable with the help of a function. I see two ways to do it and would
like to discuss its properties with respect to being Mathematicaesque,
performance, good-practice, etc.

In[27]:= f[x_] := x^2

In[28]:= g[x_] := (x = x^2)

In[29]:= SetAttributes[g, HoldFirst]

In[30]:= a = 2; b = 2;

In[31]:= a = f[a]; a

Out[31]= 4

In[32]:= g[b]; b

Out[32]= 4

For this very simple example the answer is pretty clear to me. I would
prefer f over g, e.g. f can be used with literals. But what if the
function performs a costy operation? Is changing variables in place
common practice?

Thanks a lot,

Sebastian


  • Prev by Date: List to infix
  • Next by Date: Re: Shading in Plot3D
  • Previous by thread: Re: List to infix
  • Next by thread: Radiative Transfer