Re: HoldFirst, Unevaluated
- To: mathgroup at smc.vnet.net
- Subject: [mg109618] Re: HoldFirst, Unevaluated
- From: Raffy <adraffy at gmail.com>
- Date: Sat, 8 May 2010 07:07:38 -0400 (EDT)
- References: <hs0pp2$dsn$1@smc.vnet.net>
On May 7, 3:24 am, Fred Klingener <gigabitbuc... at BrockEng.com> wrote: > Here, the second execution of f fails because variable 'a' evaluates > to Pi and can't be reassigned: > > ClearAll[f, a, b, c] > f[x_, y_, z_] := x = Pi; 3 y + 2 z > f[a, b, c] > f[a, b, c] > > I have come to understand that the canonical approach to control this > is to assign the HoldFirst attribute to f: > > ClearAll[f, a, b, c] > SetAttributes[f, HoldFirst] > f[x_, y_, z_] := x = Pi; 3 y + 2 z > f[a, b, c] > f[a, b, c] > a > > and, indeed, this works as intended. > > Plan B, using an explicit Unevaluated also works: > > ClearAll[f, a, b, c] > f[x_, y_, z_] := x = Pi; 3 y + 2 z > f[Unevaluated[a], b, c] > f[Unevaluated[a], b, c] > a > > An alternate function formulation fails, evidently in the same way as > the first example: > > ClearAll[f, a, b, c] > SetAttributes[f, HoldFirst] > f = (#1 = Pi; 3 #2 + 2 #3) & > f[a, b, c] > f[a, b, c] > a > > while > > ClearAll[f, a, b, c] > f = (#1 = Pi; 3 #2 + 2 #3) & > f[Unevaluated[a], b, c] > f[Unevaluated[a], b, c] > a > > does. > > What am I missing? > > TIA, > Fred Klingener Function[{a, b, c}, a = Pi; 3 b + 2 c, HoldFirst]