Considering the richness of Mathematica.SE, I am quite surprised I could not find my answer... I am very sorry if it is a duplicate.
Consider the following example:
With[{t1=0,t2=t1},t1+t2] (* -> t1 *)
I thought it would print 0
instead of t1
.
Indeed, a Trace
gives:
Trace@With[{t1 = 0, t2 = t1}, t1 + t2]
(* {With[{t1=0,t2=t1},t1+t2],0+t1,t1} *)
t1
stays unevaluated.
Considering the complex usage of Hold and such attributes for a Mathematica beginner like me and the documentation that states With
has attribute HoldAll
, I tried inserting Evaluate
at various places, and also tried replacing With
by Module
(I admit the difference between With
and Module
is not obvious to me), but nothing printed me 0
instead of t1
.
How can I inform Mathematica to evaluate t2=t1
with t1
and t2
declared within With
?
Edit: Thanks to the first comments and answer, some usefull and closely related question and answers can be found here.
With
s if you don't want to tryBlock
:With[{t1 = 0}, With[{t2 = t1}, (t1 + t2)]]
$\endgroup$Module[{x=0,y=x},Print[y]]
and it will printx
and not0
. Mathematica does not update the parameterson the fly
. Hence it does not use the updated value just assigned to. This is how it always been. $\endgroup$p1=p2
at all in the local variables list. If you must setp2
top1
, do that after the parameter list, here-->Module[{p1=0,p2}, p2=p1....]
You just have to remember this. $\endgroup$With
" before reading this very interesting q/a you're refering to. In fact, I was just reading this one during you commenting. $\endgroup$