Read Online
Download PDF
Additional resources
Revision history
Known typos/bugs
Report a bug
License terms
About the author
"node407_1.gif" "node407_2.gif" "node407_3.gif" The meaning of argument holding

These attributes are used when some of the function arguments have to be evaluated only after the rules associated with the function name have been applied. This means that these attributes change the evaluation order from standard evaluation (depth-first, subexpressions before expressions) to a non-standard one (expressions before subexpressions). One usually needs to change the evaluation order to do something non-trivial. In particular, as we have seen already on the example of the increment function <inc[x]> (sections 2.5.5, 4.5.2),  Hold attributes can be used to mimic the pass-by-reference semantics. This allows functions to modify the variables which are passed to them. Other cases when one needs to hold some arguments unevaluated arise when only some of the arguments have to be evaluated at all, and which ones have to be evaluated is decided by say a condition on the part of arguments that are evaluated (this is exactly the situation with conditional operators such as If).

The attribute HoldFirst instructs a function to hold (in unevaluated form) the first argument. HoldRest instructs to hold all but the first argument, and HoldAll instructs to hold all arguments. The fact that the argument is held unevaluated does not necessarily mean  that it is never evaluated in a function (which may also happen if it is discarded before it is evaluated) - it simply means that it is evaluated after all the transformations of this argument by the function <f> ( according to the definition of <f>) are performed. As a simple example, consider a squaring function:


Let us Trace its evaluation on some number :



We see that < a > was evaluated before < f > . Now let us attach the HoldFirst attribute to < f > :


Now :



We see that now the evaluation order has changed : first the function < f > was evaluated, and then the value fo < a > was substituted. In this simple example, the end result was the same regardless of the evaluation order, but in less trivial cases the evaluation order becomes important.

It is fairly easy to give an example of held arguments being discarded and thus not evaluated at all - take any operators on the False branch of some If operator.

"node407_10.gif" "node407_11.gif" "node407_12.gif"

Created by Wolfram Mathematica 6.0  (05 February 2009) Valid XHTML 1.1!