NHoldAll, NHoldFirst, NHoldRest

The next cell gives (f) the NHoldRest attribute.

ClearAll[f] ;    Attributes[f] = {NHoldRest} ;


Now only the first argument of (f) can be evaluated numerically.

N[f[π/2, π/3, π/4]]

f[1.5708, π/3, π/4]


NHoldAll, NHoldRest and NHoldFirst prevent evaluation even deep inside an
expression as demonstrated in the next cell.

N[{π + {f[π/2, π/3, π/4]}}]

{{3.14159 + f[1.5708, π/3, π/4]}}


Built-in symbols with attributes NHoldAll, NHoldFirst, NHoldRest

The next cell makes a list of all built-in symbols.

symbs = Cases[ToExpression/@Names["System`*"], _Symbol] ;


The next cell returns all built-in symbols with the NHoldAll attribute.  If
you understand what C and Root do it's easy to see why they have the
attribute NHoldAll.

Select[symbs, MemberQ[Attributes[#], NHoldAll] &]

{C, Root}


The next cell returns all built-in symbols with the NHoldRest attribute.  If
these functions didn't have the NHoldRest attribute they wouldn't work
correctly in certain cases.

Select[symbs, MemberQ[Attributes[#], NHoldRest] &]

{Drop, Extract, HeldPart, Part, Take}


The next cell returns all built-in symbols with the NHoldFirst attribute.  I
have no experience with the functions that have the NHoldFirst attribute.

Select[symbs, MemberQ[Attributes[#], NHoldFirst] &]

{EllipticTheta, EllipticThetaPrime, MathieuC, MathieuCharacteristicA, MathieuCharacteristicB, MathieuCPrime, MathieuS, MathieuSPrime, PolyGamma, StieltjesGamma}


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page