# How does Mathematica interpret a expression?

I'm a beginner to Mathematica. I'd read several document. But I still have a problem. For example, every expression in Mathematica are of the form Head[expr1,expr2,...] If I type Plus[3^3,6*9], what is the practical order Mathematica interpret?

1. Interpret Plus first, and then interpret expr1, expr2,... In other words, from 'outside' to 'inside'
2. Evaluate Power[3,3] first, then Times[6,9], after evaluating these, then interpret Plus

Or there may be other ways Mathematica use? What else should I notice? Thanks for your replying and forgiving my bad English grammar. :-)

• Order of evaluation can get complicated but in general it is inside out. Here #2 is correct. Take a look at Plus[3^3, 6*9] // Trace – mfvonh Jun 24 '14 at 5:32
• reference.wolfram.com/mathematica/tutorial/… – Szabolcs Jun 24 '14 at 5:33
• Take a look at this Q&A: mathematica.stackexchange.com/q/29339/131. – Yves Klett Jun 24 '14 at 5:34
• reference.wolfram.com/mathematica/tutorial/… – Szabolcs Jun 24 '14 at 5:34
• It evaluates the head by itself to decide what to do with the whole expression. Try this: p = Plus; p[3^3, 6*9] // Trace. Notice how it evalutes p, then it evalutes what's inside, then it evaluates the whole expression including the head and the interior elements. That's how, for example, it knows not to evaluate the things inside Hold. – mfvonh Jun 24 '14 at 6:07

Maybe this type of visualization will help understand the evaluation order better. Things are printed in InputForm here, but please "think" FullForm when you look at expressions.

In:= On[]
Plus[3^3,6*9]
Off[]
During evaluation of In:= On::trace: On[] --> Null. >>
During evaluation of In:= Power::trace: 3^3 --> 27. >>
During evaluation of In:= Times::trace: 6 9 --> 54. >>
During evaluation of In:= Plus::trace: 3^3+6 9 --> 27+54. >>
During evaluation of In:= Plus::trace: 27+54 --> 81. >>
Out= 81

In:= p=Plus
Out= Plus

In:= On[]
p[3^3,6 9]
Off[]
During evaluation of In:= On::trace: On[] --> Null. >>
During evaluation of In:= p::trace: p --> Plus. >>
During evaluation of In:= Power::trace: 3^3 --> 27. >>
During evaluation of In:= Times::trace: 6 9 --> 54. >>
During evaluation of In:= Plus::trace: p[3^3,6 9] --> 27+54. >>
During evaluation of In:= Plus::trace: 27+54 --> 81. >>
Out= 81


This shows the evaluation order clearly and precisely. In the second example,

1. evaluate p -> Plus
2. 3^3 -> 27
3. 6*9 -> 54
4. p[3^3,6 9] -> Plus[27, 54]
5. 27+54 -> 81

TracePrint shows the same information, but perhaps it's not quite as clear:

In:= TracePrint[p[3^3,6 9]]
During evaluation of In:=  p[3^3,6 9]
During evaluation of In:=   p
During evaluation of In:=   Plus
During evaluation of In:=   (3^3)
During evaluation of In:=    Power
During evaluation of In:=    3
During evaluation of In:=    3
During evaluation of In:=   27
During evaluation of In:=   6 9
During evaluation of In:=    Times
During evaluation of In:=    6
During evaluation of In:=    9
During evaluation of In:=   54
During evaluation of In:=  27+54
During evaluation of In:=  81
Out= 81


It also mentions subexpressions that evaluate to themselves (i.e. don't evaluate), e.g. Times -> Times or 6 -> 6.

The evaluation sequence is documented in detail here:

• Thanks! But in your last paragraph, I still can't see why does TracePrint[a + b] prints a->b->a+b then not again a+b?(not like TracePrint[3+2]). Is it because there's no probability that can make a+b turn into another expression? I think I have been familiar with the evaluation order now. But that point confuse me~ – Eric Jun 27 '14 at 14:36
• TracePrint[a + b] prints Plus[a,b] first because that's where it starts. Then, it takes each element of this expression one by one. Note that the next three lines it prints are indented by a space, meaning that these are part of the expression printed before them. Now it prints Plus, meaning Plus evaluates to itself a, meaning a evaluates to itself, b, meaning b evaluates to itself. Done. Any expression that doesn't change anymore is printed only once. Thus Plus, a, b and Plus[a,b] are all printed only once. – Szabolcs Jun 27 '14 at 14:45
• Is it different from 3+2? TracePrint[3 + 2] prints Plus[3,2] first. Then it prints Plus, meaning Plus evaluates to itself, 3 meaning 3 evaluates to itself, 2, meaning 2 evaluates to itself. Then done. – Eric Jun 27 '14 at 15:00
• @Eric yes, it's different. After the evaluator made sure that it's done with all subparts of 3+2 (i.e. Plus, 3, 2), it's still not yet done with 3+2 itself: it is at this point that it carries out the addition. There's an extra line saying 5 at the end. – Szabolcs Jun 27 '14 at 15:06
• Oh, I suddenly realize it. The reason why Plus[a,b] and Plus[3,2] take different steps is: Plus[a,b] evaluate Plus first, then a and b, however, at the very time, by the rule defined for Plus, it doesn't have a rule corresponding to a symbol plus a symbol, so it keeps its form being Plus[a,b]. – Eric Jun 27 '14 at 15:43