Re: Evaluate/Module
- To: mathgroup at smc.vnet.net
- Subject: [mg16119] Re: Evaluate/Module
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Sat, 27 Feb 1999 03:22:58 -0500
- Organization: University of Western Australia
- References: <7atq9s$7kr@smc.vnet.net> <7b32nl$1ss@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Allan Hayes wrote:
> Neither problem invoves Module; both are due to how expressions like
> e1;e2;e3;e4 (FullForm CompoundExpression[e1,e2, e3,e4]
> evaluate
>
> Normally, CompoundExpression evaluates its entries in sequence and then
> gives the value of the last one. However, since it has the attribute HoldAll
> this evaluation is under the control of the programmer and if some entries
> are wrapped in Evaluate[ ] they will be evaluated first *and* the 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*
I'm not convinced by your explanation. Consider the following
CompoundExpressions:
In[1]:= a = 1; a = a - 1; Evaluate[a]
Out[1]= 0
In[2]:= a = 1; a = a - 1; a
Out[2]= 0
Now, compare these results with a simple Module:
In[3]:= Module[{a = 1}, a = a - 1; Evaluate[a]]
Out[3]= 1
In[4]:= Module[{a = 1}, a = a - 1; a]
Out[4]= 0
Note that both Module and CompoundExpression have the HoldAll Attribute:
In[5]:= Attributes[Module]
Out[5]= {HoldAll, Protected}
In[6]:= Attributes[CompoundExpression]
Out[6]= {HoldAll, Protected, ReadProtected}
Using Trace does give some idea as to what is going on:
In[7]:= Trace[a = 1; a = a - 1; Evaluate[a]]
Out[7]= {{a, 0}, a = 1; a = a - 1; 0, {a = 1, 1},
{{{a, 1}, 1 - 1, 0}, a = 0, 0}, 0}
In[8]:= Trace[a = 1; a = a - 1; a]
Out[8]= {a = 1; a = a - 1; a, {a = 1, 1},
{{{a, 1}, 1 - 1, 0}, a = 0, 0}, {a, 0}, 0}
In[9]:= Trace[Module[{a = 1}, a = a - 1; a]]
Out[9]= {Module[{a = 1}, a = a - 1; a], {a$9 = 1, 1},
{a$9 = a$9 - 1; a$9, {{{a$9, 1}, 1 - 1, 0}, a$9 = 0, 0},
{a$9, 0}, 0}, 0}
In[10]:= Trace[Module[{a = 1}, a = a - 1; Evaluate[a]]]
Out[10]= {Module[{a = 1}, a = a - 1; Evaluate[a]], {a$10 = 1, 1},
{{a$10, 1}, a$10 = a$10 - 1; 1,
{{{a$10, 1}, 1 - 1, 0}, a$10 = 0, 0}, 1}, 1}
Cheers,
Paul
____________________________________________________________________
Paul Abbott Phone: +61-8-9380-2734
Department of Physics Fax: +61-8-9380-1014
The University of Western Australia
Nedlands WA 6907 mailto:paul at physics.uwa.edu.au
AUSTRALIA http://www.physics.uwa.edu.au/~paul
God IS a weakly left-handed dice player
____________________________________________________________________