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$
  • $\begingroup$ To give you an idea: try t=x; With[{x=1}, {x, t}]. $\endgroup$ – J. M.'s discontentment Nov 11 '15 at 14:05
4
$\begingroup$

I think you are confusing the behaviour of With and Block

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

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