1
$\begingroup$

Why does

With[{x0 = 1, y0 = 2}, 
  f[x_] = a + b x /. First[
   Solve[{y0 == a + b x0, b == 0.1}, {a, b}]]]

evaluate to

1.9 + 0.1 x$

instead of

1.9 + 0.1 x

?

$\endgroup$
10
  • $\begingroup$ See the Details on With and this comment by jkuczm. It's the expected behavior. $\endgroup$
    – b3m2a1
    Commented Dec 21, 2017 at 15:24
  • $\begingroup$ note that f gets defined as intended. Only the return value of with is odd and you can hide it with a semicolon. $\endgroup$
    – george2079
    Commented Dec 21, 2017 at 15:38
  • $\begingroup$ I think that in the Details section of the documentation, the relevant entry is about nested With statements which is obviously not the case here. Similarly, the linked comment verifies that it's not clear when renaming occurs in nested statements. I think that one seemingly relevant example in section Properties & Relations (the last one) is actually not relevant here; same applies for the examples in the Possible Issues section $\endgroup$
    – user42582
    Commented Dec 21, 2017 at 15:41
  • $\begingroup$ @george2079 yes that's true; I actually encountered this behaviour when answering another question a few minutes ago; I am curious as to why it happens; the code is perfectly operational otherwise $\endgroup$
    – user42582
    Commented Dec 21, 2017 at 15:43
  • 1
    $\begingroup$ @user42582 With doesn't care whether the scoping construct is With or a function definition. It's all the same thing (lexical rescoping). $\endgroup$
    – b3m2a1
    Commented Dec 21, 2017 at 16:50

0

Browse other questions tagged or ask your own question.