Unevaluated


When Unevaluated[expr] is an argument of a function (expr) is given to the
function it is an argument of without evaluating.  Consider the example in
the next cell.  If it were not for the use of Unevaluated the sum would
evaluate to 30 which has the head Integer.  Instead the head Plus is
returned.

Head[Unevaluated[2 + 3 + 5^2]]

Plus


Often times when we make a function with a holding attribute (e.g.  HoldAll,
HoldRest, HoldFirst) we have to use Unevaluated in the definition of the
function.  Consider the function in the next cell that converts it's argument
to a string before the string can evaluate.



Here the HoldAll attribute prevents MakeString from evaluating it's argument.
However, we also need to use Unevaluated to ensure ToString doesn't evaluate
it's argument.

ClearAll[MakeString]    Attributes[MakeString] = {HoldAll} ;    MakeString[a_] := ToString[Unevaluated[a]] <>" is a string"


The next cell demonstrates the MakeString function.  We see that (mass) is
converted to a string even though the symbol has a numeric value assigned to
it.

mass = 56 ;  {mass, MakeString[mass]}

{56, mass is a string}

For more understanding of Unevaluated see examples in my explanation of  the Mathematica evaluation process.


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page