       HoldFirst, Unevaluated

• To: mathgroup at smc.vnet.net
• Subject: [mg109600] HoldFirst, Unevaluated
• From: Fred Klingener <gigabitbucket at BrockEng.com>
• Date: Fri, 7 May 2010 06:30:47 -0400 (EDT)

```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

```

• Prev by Date: Re: NDSolve backward in time
• Next by Date: RuleDelayed for parsing XML with multiple children
• Previous by thread: Re: matrix equation
• Next by thread: Re: HoldFirst, Unevaluated