1
$\begingroup$

This question already has an answer here:

Evaluating the following piece of code, returns-as expected-3:

g[a_] = a;
With[{a = 1},
  2 + g[a]
 ]

Why does the following snippet produce the counter-intuitive result 2+a?

f = a;
With[{a = 1},
  2 + f
 ]

Is the later case an example of a use case where using Block is preferable?

edit

The direct motivation for this question, comes from the solution I proposed to this question, here.

There, I needed a way to initialize numerically matrix H. In that case, I had to revert to using ReplaceAll because a construct like With proved a bad choice; alternatively, Block might have been a better/more straightforward decision-hence the second part of this question.

An indirect motivation comes from this question, where it seems that With behaves in an unexpected way; that consideration prompted the first part of this question.

Having said all that, I hope that it is now fairly obvious that the question is more about practical considerations rather than theoretical/syntactical issues; I am interested in how a seasoned Mathematica programmer would tackle the issues codified in the toy example presented in the body of the question and -hopefully- exemplified in this postscript.

$\endgroup$

marked as duplicate by Daniel Lichtblau, Kuba Jan 21 '18 at 15:56

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.

  • 2
    $\begingroup$ Have you tried using Trace? Once you get used to its peculiar output, Trace becomes very useful for this king of issues. By the way, the answer is in the manual: 'With allows you to define local constants.' It is the first entry in the details section. $\endgroup$ – Hector Jan 21 '18 at 14:38
  • $\begingroup$ WReach's answer perfectly fits here: mathematica.stackexchange.com/a/633/5478 do you agree? $\endgroup$ – Kuba Jan 21 '18 at 15:42
  • $\begingroup$ @user42582 I assume it was you who voted to reopen. After reading your update, I still do not understand why the linked duplicate doesn't address the question. It makes it clear that Block will give you the answer you were expecting, and also explains why With doesn't. If you believe it should be reopened (the purpose of which is to allow posting answers), it is better to give a convincing explanation than just cast a vote. $\endgroup$ – Szabolcs Jan 21 '18 at 16:51
  • $\begingroup$ @Szabolcs thanks for clarifying; I'd gladly delete the question should you advise so; I though I was supposed to vote reopen to signal the edit $\endgroup$ – user42582 Jan 21 '18 at 17:11
  • $\begingroup$ You don't have to delete a question just because it got closed. It won't affect your reputation score. Reopening would be necessary if you feel that your question wasn't fully answered by the duplicate. What I was trying to say was that in that case, you should explain why the answer in the duplicate didn't clear up everything. Then people will be able to understand what kind of answer you are looking for, and will give you the required number of reopen votes. $\endgroup$ – Szabolcs Jan 21 '18 at 17:29

Browse other questions tagged or ask your own question.