Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
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 at 5:32
    
    
Take a look at this Q&A: mathematica.stackexchange.com/q/29339/131. –  Yves Klett Jun 24 at 5:34
    
1  
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 at 6:07

1 Answer 1

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[]
Plus[3^3,6*9]
Off[]
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]
Off[]
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:

share|improve this answer
    
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 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 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 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 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 at 15:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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