In my previous question I described a problem, when one of the approaches I tried to use didn't work because of the interaction between Evaluate[] in combination with SetDelayed[].
Suppose we have a symbol defined in a global scope:
x = 1; And now we defined a function:
f[x_] := x + 1 In this case Mathematica understands that x refers to the function argument and not to the global symbol:
?f (* Definitions: f[x_] := x + 1 *) Now I want to define one function in terms of another:
f[x_, a_] := a x f1[x_] := f[x, 1] + 1 ?f1 (* Definitions: f1[x_] := f[x, 1] + 1 *) As expected again x here refers to the function argument.
Now I want to simplify the definition of f1, so that it looks like:
(* Definitions: f1[x_] := 1 + x *) and I use Evaluate[]:
f1[x_] := Evaluate[f[x, 1] + 1] But instead of 1 + x I get:
(* Definitions: f1[x_] := 2 *) Because when applying Evaluate[] to an expression containing x Mathematica thinks that I'm referring to a global x rather than the function argument.
Why does Evaluate[] work like this? Is there any way to override the scope of the x?
fis now evaluated immediately, and hence will use the globalx, and not when the functionfis called in order to use its argumentxinstead? In effect, writingf1[x_] := Evaluate[f[x, 1] + 1]is as if you have writtenf1[x_] = f[x, 1] + 1]countering the whole reason to use the delayed assignment,. $\endgroup$