Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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.

share|improve this question

marked as duplicate by Simon Woods, rcollyer, Kuba, Artes, Sjoerd C. de Vries Aug 27 '13 at 15:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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)]] – gpap Aug 27 '13 at 10:21
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. – Nasser Aug 27 '13 at 10:24
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. – Nasser Aug 27 '13 at 10:37
@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. – max Aug 27 '13 at 13:14
@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? – max Aug 27 '13 at 13:17

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

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


share|improve this answer
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 :) – Nasser Aug 27 '13 at 12:19
@Nasser what are the Attributes of Module? There in lies your answer. :P – rcollyer Aug 27 '13 at 13:27

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