From: Fred Klingener on
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

From: Raffy on
On May 7, 3:24 am, Fred Klingener <gigabitbuc...(a)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]

From: Albert Retey on
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