Re: Evaluate/Module
- To: mathgroup at smc.vnet.net
- Subject: [mg16106] Re: Evaluate/Module
- From: Mark Fisher <mefisher at bellsouth.net>
- Date: Thu, 25 Feb 1999 08:25:01 -0500
- References: <7atq9s$7kr@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Bernd,
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:
Thanks.
--Mark.
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