MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Evaluate/Module[Correction]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16145] Re: Evaluate/Module[Correction]
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sat, 27 Feb 1999 03:23:11 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

Bernd Brandt has emailed me that he does not get the result that made me
think that I had got the evaluation of CompoundExpression wrong. On
re-evaluating the example I don't either  -- there is a typo in my posting
which may be related.
I give variant of the example (In[1], below). This supports the original
account, which with apologies to all, I now revert to:

Normally, CompoundExpression evaluates its entries in order and then gives
the value of the last one. However, it has the attribute HoldAll, and if
some entries are wrapped in Evaluate[ ] they will be evaluated first, in
order, *and* the resulting expression will then be evaluated as usual.
So, writing ei* for the value of ei, we get the following steps

CompoundExpression[e1, Evaluate[e2], Evaluate[e3], e4]
CompoundExpression[e1, e2*, Evaluate[e3], e4]
CompoundExpression[e1, e2*, e3*, e4]      (*restart!*)
CompoundExpression[e1*, e2*, e3*, e4]
CompoundExpression[e1*, e2**, e3*, e4]
CompoundExpression[e1*, e2**, e3**, e4]
CompoundExpression[e1*, e2**, e3**, e4*]
e4*

In[1]:=
pr=.;

In[2]:=
CompoundExpression[ 
    { Print[2], pr = Print },  Evaluate[ {Print[1], pr[3]} ],  pr[4] 
]

1
2
3
4

The 3 comes from the second evaluatio of the second entry

Bernd also asked in his email about the following result

In[3]:=
t=x^2+1;
Module[{x=4}, Evaluate[t=t-1;Print[t]; Evaluate[t]]]
\!\(x\^2\)
Out[3]=
17

The evaluation steps for this (with M for Module, Pr for Print) are:

The outer Evaluate cancels the HoldAll attribute of Module and the CompoundExpression 
 t=t-1;Print[t]; Evaluate[t]] 
then evaluates , starting with the last entry:

    M[{x=4}, t=t-1;Pr[t]; x^2+1]
    M[{x=4}, t=x^2+1-1;Pr[t]; x^2+1]
    M[{x=4}, t=x^2;Pr[t]; x^2+1 ]
    M[{x=4}, x^2;Pr[t]; x^2+1 ]   (t=x^2  stored)
    M[{x=4}, x^2;Null; x^2+1 ],        x^2 printed
    M[{x=4},  x^2+1 ]
     
Compound Expression has now been evaluated           
Module now evaluates
    Module[{x$n=4},  x$n^2+1 ],
     x$n^2+1 ,                       
  (x$n=4 stored)
    4^2 +1
    17  (output)

This reversion to the orignal description of the evaluation of
CompoundExpression necessitates a change to my account in the previous
posting of the evaluation of one of Paul Abbot's examples:

In[1]:=
a = 1; a = a - 1; Evaluate[a]
Out[1]=
0

Evaluation steps (with CE for CompoundExpression)

    CE[a = 1, a = a - 1, a]
    CE[1, a = a - 1, a]               (a =1  stored)
    CE[1, a = 1- 1, a  ]                        ,,
    CE[1,  a = 0, a  ]                           ,,
    CE[1, 0, a  ]                        (a=0 stored)
    CE[1, 0, 0  ]                                 ,,
    0

Previously the last two lines were

    a                                                 ,,
    0


---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565





  • Prev by Date: RE: ?
  • Next by Date: Re: Early out with Map?
  • Previous by thread: Using "Fit"
  • Next by thread: Re: Evaluate/Module[Correction]