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. :-)

  • $\begingroup$ 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 $\endgroup$ – mfvonh Jun 24 '14 at 5:32
  • $\begingroup$ reference.wolfram.com/mathematica/tutorial/… $\endgroup$ – Szabolcs Jun 24 '14 at 5:33
  • $\begingroup$ Take a look at this Q&A: mathematica.stackexchange.com/q/29339/131. $\endgroup$ – Yves Klett Jun 24 '14 at 5:34
  • $\begingroup$ reference.wolfram.com/mathematica/tutorial/… $\endgroup$ – Szabolcs Jun 24 '14 at 5:34
  • 2
    $\begingroup$ 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. $\endgroup$ – 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[2]:= On[]
During evaluation of In[2]:= On::trace: On[] --> Null. >>
During evaluation of In[2]:= Power::trace: 3^3 --> 27. >>
During evaluation of In[2]:= Times::trace: 6 9 --> 54. >>
During evaluation of In[2]:= Plus::trace: 3^3+6 9 --> 27+54. >>
During evaluation of In[2]:= Plus::trace: 27+54 --> 81. >>
Out[3]= 81

In[5]:= p=Plus
Out[5]= Plus

In[6]:= On[]
p[3^3,6 9]
During evaluation of In[6]:= On::trace: On[] --> Null. >>
During evaluation of In[6]:= p::trace: p --> Plus. >>
During evaluation of In[6]:= Power::trace: 3^3 --> 27. >>
During evaluation of In[6]:= Times::trace: 6 9 --> 54. >>
During evaluation of In[6]:= Plus::trace: p[3^3,6 9] --> 27+54. >>
During evaluation of In[6]:= Plus::trace: 27+54 --> 81. >>
Out[7]= 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[9]:= TracePrint[p[3^3,6 9]]
During evaluation of In[9]:=  p[3^3,6 9]
During evaluation of In[9]:=   p
During evaluation of In[9]:=   Plus
During evaluation of In[9]:=   (3^3)
During evaluation of In[9]:=    Power
During evaluation of In[9]:=    3
During evaluation of In[9]:=    3
During evaluation of In[9]:=   27
During evaluation of In[9]:=   6 9
During evaluation of In[9]:=    Times
During evaluation of In[9]:=    6
During evaluation of In[9]:=    9
During evaluation of In[9]:=   54
During evaluation of In[9]:=  27+54
During evaluation of In[9]:=  81
Out[9]= 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:

  • $\begingroup$ 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~ $\endgroup$ – Eric Jun 27 '14 at 14:36
  • $\begingroup$ 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. $\endgroup$ – Szabolcs Jun 27 '14 at 14:45
  • $\begingroup$ 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. $\endgroup$ – Eric Jun 27 '14 at 15:00
  • $\begingroup$ @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. $\endgroup$ – Szabolcs Jun 27 '14 at 15:06
  • $\begingroup$ 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]. $\endgroup$ – Eric Jun 27 '14 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.