1
$\begingroup$

The command With[{x = 1}, x] outputs 1.

The command t = x; With[{x = 1}, t] outputs x.

I expected this second input to give the same answer. How can I modify my code so it behaves in this way?

$\endgroup$
1
  • $\begingroup$ To give you an idea: try t=x; With[{x=1}, {x, t}]. $\endgroup$ Nov 11, 2015 at 14:05

2 Answers 2

4
$\begingroup$

I think you are confusing the behaviour of With and Block

t = x; Block[{x = 1}, t]
(* 1 *)
$\endgroup$
1
  • $\begingroup$ Yes, this works better for my more complicated actual situation. Thanks :) $\endgroup$
    – Tom
    Nov 11, 2015 at 14:42
5
$\begingroup$
t = x; With[{x = 1}, Evaluate[t]]
$\endgroup$
1
  • $\begingroup$ Nice. Thank you $\endgroup$
    – Tom
    Nov 11, 2015 at 14:07

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