4
$\begingroup$

How can I tell programmatically if a function has evaluated in a nontrivial way?

I'd like to do, essentially, something along the lines of f[x]===HoldForm[f[x]], a test that returns true if f[x] just evaluates to f[x] because it's not defined, or else false if f[x] actually does something. Of course, that example code doesn't work - my guess is that the HoldForm is printing itself or something.

$\endgroup$
4
$\begingroup$

f[x] === HoldForm[f[x]] does not provide the desired result, because HoldForm stays attached to f[x] on the right side of the expression. Thus, the left and right sides never are identical. Instead use,

f[x] === Unevaluated[f[x]]
(* True *)

And, when the function does something,

g[x_] := x^2
g[x] === Unevaluated[g[x]]
(* False *)

Unevaluated prevents evaluation only on a single line, then goes away, so it works for this purpose.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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