Given a symbol t and an expression expr, how can I determine whether or not the symbol t appears somewhere in expr?
The best solution I have up with so far is:
Block[{t,s},(expr/.t->s)=!=expr]
which will return True if t is in expr, and False otherwise.
But this feels a bit like a hack because it's not really using /. because it's the right tool, but rather because /. happens to need to search through expr in order to do its unrelated task. This results in having to search through expr at least three times (I think?): once for the /., and twice for each side of the =!=, when clearly its possible to find t in only one search.
Answer
Try FreeQ
FreeQ[x^2, t]
(*True*)
FreeQ[x^2, x]
(*False*)
Comments
Post a Comment