       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:= a = 1; a = a - 1; Evaluate[a]
Out= 0

In:= a = 1; a = a - 1; a
Out= 0

Now, compare these results with a simple Module:

In:= Module[{a = 1}, a = a - 1; Evaluate[a]]
Out= 1

In:= Module[{a = 1}, a = a - 1; a]
Out= 0

Note that both Module and CompoundExpression have the HoldAll Attribute:

In:= Attributes[Module]
Out= {HoldAll, Protected}

In:= Attributes[CompoundExpression]

Using Trace does give some idea as to what is going on:

In:= Trace[a = 1; a = a - 1; Evaluate[a]]
Out= {{a, 0}, a = 1; a = a - 1; 0, {a = 1, 1},
{{{a, 1}, 1 - 1, 0}, a = 0, 0}, 0}

In:= Trace[a = 1; a = a - 1; a]
Out= {a = 1; a = a - 1; a, {a = 1, 1},
{{{a, 1}, 1 - 1, 0}, a = 0, 0}, {a, 0}, 0}

In:= Trace[Module[{a = 1}, a = a - 1; a]]
Out= {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:= Trace[Module[{a = 1}, a = a - 1; Evaluate[a]]]
Out= {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
____________________________________________________________________

```

• Prev by Date: Using Utilities`Notation` in a package
• Next by Date: Re: Reset In[ ] & Out[ ]
• Previous by thread: Re: Evaluate/Module
• Next by thread: How can I plot for array variable?