2
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ There are really good explanations of lexical and dynamic scoping here and here. In your example, you can nest two Withs if you don't want to try Block: With[{t1 = 0}, With[{t2 = t1}, (t1 + t2)]] $\endgroup$
    – gpap
    Aug 27, 2013 at 10:21
  • $\begingroup$ This is well known. This is how Mathematica works. nothing new. Try Module[{x=0,y=x},Print[y]] and it will print x and not 0. Mathematica does not update the parameters on the fly. Hence it does not use the updated value just assigned to. This is how it always been. $\endgroup$
    – Nasser
    Aug 27, 2013 at 10:24
  • 1
    $\begingroup$ The rule of thumb I use, so that I do not make a mistake like this, (thinking I set some variable to some value, but it is not), is to never use p1=p2 at all in the local variables list. If you must set p2 to p1, do that after the parameter list, here--> Module[{p1=0,p2}, p2=p1....] You just have to remember this. $\endgroup$
    – Nasser
    Aug 27, 2013 at 10:37
  • $\begingroup$ @SimonWoods it is not exactly the same question as some people have to know the answer of this one: "Nest your With" before reading this very interesting q/a you're refering to. In fact, I was just reading this one during you commenting. $\endgroup$
    – max
    Aug 27, 2013 at 13:14
  • $\begingroup$ @Nasser Thank you I will be using your advice as soon as now. Don't you think it is worth putting it as an answer so that I can close the question? $\endgroup$
    – max
    Aug 27, 2013 at 13:17

1 Answer 1

2
$\begingroup$

LetL do exactly what you want. You can see it implementation here.

LetL[{t1 = 0, t2 = t1}, t1 + t2]

0

$\endgroup$
2
  • $\begingroup$ How would one use LetL for With and Module? Module[{LetL[{a = 1, b = a + 1, c = a + b + 2}, {a, b, c}]}, Print[a]; ] did not work, and Module[LetL[{a = 1, b = a + 1, c = a + b + 2}, {a, b, c}], Print[a]; ] did not work. Is there a different syntax to use? The question is asking on using this in the context of With and Module, so I assume one can do that, I just can't figure the syntax :) $\endgroup$
    – Nasser
    Aug 27, 2013 at 12:19
  • $\begingroup$ @Nasser what are the Attributes of Module? There in lies your answer. :P $\endgroup$
    – rcollyer
    Aug 27, 2013 at 13:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.