MathGroup Archive 2007

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

Search the Archive

Re: How to manipulate globals in a function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73710] Re: How to manipulate globals in a function?
  • From: "oshaughn" <oshaughn at northwestern.edu>
  • Date: Sun, 25 Feb 2007 04:36:55 -0500 (EST)
  • References: <erop34$94v$1@smc.vnet.net>

This is an order-of-evaluation issue : when mathematica evaluates a
function, the arguments are evaluated *first*.
So for example if I have
  f[a_,b_] := a=b
then
  a=3
  Trace[ f[a,2]]
returns an error, because the first step in evaluation is to replace
'a' by 3, before the function is called.
 {{a, 3}, f[3, 2],  3 = 2, {Message[Set::
    setraw, 3], {Set::setraw, Cannot assign to raw object `1`.},


However, the mathematica attribute HoldFirst fixes this problem,
preventing the first argument from being evaluated "too soon"
   SetAttributes[f, HoldFirst]
   Trace[f[a,2]]
   --> {f[a, 2], a = 2, 2}

On Feb 24, 1:22 am, davi... at gmail.com wrote:
> I am trying to write code to manipulate a global array in a function.
> How do something like this:
>
> Moo [A_List, i_Integer] :=
>
>       A[[i]] = 7;
>
> Moo[A, 1]
>
> This fails.  But, if I do this without a funciton it works.
>
> i=1
> A[[i]] = 7
>
> JD




  • Prev by Date: Re: multi-threading on Mac OS X.
  • Next by Date: Re: Kernel Crashing with NDSOlve
  • Previous by thread: Re: How to manipulate globals in a function?
  • Next by thread: Re: How to manipulate globals in a function?