MathGroup Archive 1999

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

Search the Archive

Re: Evaluate/Module


I don't think Module is playing a central role here. Rather I think CompoundExpression
is. The FullForm of (a; b) is CompoundExpression[a,b].
It has the attribute HoldAll. When you write (a; Evaluate[b]) you have told
Mathematica to find out what b is *before* starting to execute the progam (a; b).
Thus if you write

tmp = x^2+1;
(tmp = tmp-1; Evaluate[tmp]) --> (tmp = tmp-1; x^2+1)

Of course, when you check tmp afterwards, you find that it is x^2 now.

When you change your code to

tmp = x^2+1;
(tmp = tmp-1; l = Evaluate[tmp])

the second argument in the CompoundExpression has the head Set, not Evaluate and hence
it does not get evaluated "out of order". You see that both tmp and l are now set to
x^2. If you had instead written

tmp = x^2+1;
(tmp = tmp-1; Evaluate[l = tmp])

you *would* have forced the evaluation of the Set statement before the reassignment of
tmp. Afterwards you will find that tmp is x^2 but l is x^2+1.

Thinking about your example helped me sharpen my understanding about this stuff:


Bernd Brandt wrote:

> Hi,
> I'm having some trouble with Evaluate and i cannot find this in the
> documentation.
> Does anybody know what and esp. why the following is happening?
>                 In1:= tmp = x^2 +1;
>                 In2:= Module[{x=4},tmp=tmp-1;Print[tmp]; Evaluate[tmp]]
>                 x^2
>                 x^2 + 1
> In the above Evaluate[tmp] somehow takes the latest value of tmp and not
> the current one after the "tmp=tmp-1". Why?
> Changing "Evaluate[tmp] to "l=Evaluate[tmp]" does give the right answer
> (in this case x^2) immediately. Does this assignment force Evaluate to
> Evaluate tmp before ending the Module. Does someone know more about
> Evaluate and Modelule?
>                 lM[x0_]:= Module[{lonediff,lMdiff,ldead,lone,search},
>                 lonediff= 1 + lone/2 - x0/lone - 2 / (1 + Exp[2*ldead - 2*lone]);
>                 lMdiff = (2*(x0/(1+x0) )*E^(-ldead+ lone))/(E^(2*lone-2*ldead)+1)-
> 0.1;
>                 search=FindRoot[{lMdiff==0, lonediff==0 },{ldead,x0+1} ,{lone,x0}];
>                 Evaluate[ldead /. search]
>                 ];
>                 lM[6]
>                 ReplaceAll::"reps":
>                   {search$77} is neither a list of replacement rules nor a valid
>                 dispatch table, and so cannot be used for replacing."
>                 5.44865
> In this module Evaluate somehow is looking for an global "search"
> variable.
> Why does it not just evaluate this local "search"?
> Thanks,
> Bernd

  • Prev by Date: RE: Re: Win 98 Printing Problems
  • Next by Date: RE: How can I plot for array variable?
  • Previous by thread: Evaluate/Module
  • Next by thread: Re: Evaluate/Module