My colleague sent me this surprising example of code today (this is after stripping everything unrelated to the evaluation):
Plot[ Evaluate[ ( x - 1) /. { x -> y}] /. i -> 1 , { x , 0 , 1}]
Note that the last substitution {i -> 1}
is done outside of Evaluate
.
Naively I expected this to produce an empty plot since the substitution replaces the plotting variable. However, the plot appears as if there is no substitution:
Whenever Evaluate
is wrapped around the entire first argument of Plot
, or the function inside Evaluate
is simplified, the plot is empty as I expected. Both
Plot[ Evaluate[ x /. x -> y ] /. i -> 1, { x, 0, 1}] (* and *)
Plot[ Evaluate[ x-1 /. x -> y /. i -> 1], {x, 0, 1}]
produce empty plots.
Update: the last substitution {i->1}
wraps Evaluate
inside ReplaceAll
which effectively cancels its action.
One question remains, why is the substitution /. {x -> y}
ignored? E.g.
Plot[ (x - 1 ) /. { x -> y} , { x, 0, 1}]
produces a plot of x - 1
, but
Plot[ x /. { x -> y}, {x, 0, 1}]
produces an empty plot.