Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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