Evaluate
only works when it is the explicit head of an argument. In other words Evaluate[ . . . ]
must appear as one of the arguments of the Head who's Hold attribute you wish to override. You should read this paper, which teaches this among many other useful things:
As an example consider these lines:
Hold[1 + 1, Evaluate[2 + 2]]
Hold[1 + 1, {Evaluate[2 + 2]}]
Hold[1 + 1, Evaluate @@ {2 + 2}]
Hold[1 + 1, 4]
Hold[1 + 1, {Evaluate[2 + 2]}]
Hold[1 + 1, Evaluate @@ {2 + 2}]
Note that only the first form evaluates. On lines two and three the Heads of the second arguments are List
and Apply
rather than Evaluate
.
In your example the outer function with a hold Attribute is SetDelayed
rather than Hold
, but the same logic applies.
A common method to get around this is to use With
to inject an expression into the body of the definition:
Clear[f]
f[x_] := x^2
With[{body = D[f[x], x]},
g[x_] := body + f[x]
]
Clear[f]
g[x]
2 x + f[x]
If you want to evaluate the entire right-hand-side, as in your first example, merely use Set
instead of SetDelayed
, and eliminate Evaluate
:
Clear[f, g]
f[x_] := x^2
g[x_] = D[f[x], x];
Clear[f]
g[x]
2 x
(If this answer seems strangely familiar that's because it is; call this an experiment in following the advice to reopen.)
Evaluate
at different levels of an expression and it's interaction withSetDelayed
. The other question is about the explicit use ofEvaluate
and it's interaction withFunction
. For experienced Mathematica users, the two question are related, so it is no coincidence that part of Mr.Wizard's answer to the other question is applicable to this question. ... $\endgroup$