Re: HoldFirst, Unevaluated

*To*: mathgroup at smc.vnet.net*Subject*: [mg109627] Re: HoldFirst, Unevaluated*From*: Albert Retey <awnl at gmx-topmail.de>*Date*: Sat, 8 May 2010 07:09:16 -0400 (EDT)*References*: <hs0pp2$dsn$1@smc.vnet.net>

Am 07.05.2010 12:24, schrieb Fred Klingener: > 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 You probably want this: ClearAll[f, a, b, c] f = Function[Null,#1 = Pi; 3 #2 + 2 #3,HoldFirst] > > 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? Attributes of Symbols affect only the downvalues you have defined for them, in the case that did not work, you did assign an ownvalue, which happens to be a function. Thus the HoldFirt did not have an effect, since the Function still did evaluate its arguments. You can set attributes to Functions as shown above... hth, albert