Appendix A: How Expressions are Evaluated
Evaluation of expressions
Evaluation takes place whenever an expression is entered. Here is the general procedure followed by Mathematica when evaluating an expression (with a few exceptions):
-
If the expression is a number or a string, it is left unchanged.
<i class="emphasis">In[1]:</i>= <b class="bold">4.58425</b><i class="emphasis">Out[1]</i>= 4.58425
-
If the expression is a symbol, it is rewritten if there is an applicable rewrite rule in the global rule base; otherwise, it is unchanged.
<i class="emphasis">In[2]:</i>= <b class="bold">expr</b><i class="emphasis">Out[2]</i>= expr<i class="emphasis">In[3]:</i>= <b class="bold">my symbol</b><i class="emphasis">Out[3]</i>= my symbol
-
If the expression is not a number, string or symbol, its parts are evaluated in a specific order:
-
The head of the expression is evaluated.
-
The arguments of the expression are evaluated in order, except when the head is a symbol with a Hold attribute. In this case, some of its arguments are left in their unevaluated forms.
-
-
After the head and arguments of an expression are each completely evaluated, the expression consisting of the evaluated head and arguments is rewritten (after making any necessary changes to the arguments based on the Attributes of the head) if there is an applicable rewrite rule in the global rule base.
-
After carrying out the previous steps, the resulting expression is evaluated in the same way and then the result of that evaluation is evaluated, and so on until there are no more applicable rewrite rules.
The term rewriting process done in steps 2 and 4 above can be described as follows:
-
pattern match parts of...