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.
FreeQ
is a terrible descriptive function name, and it's not listed in the 'Patterns' Guide, or the 'Testing Expressions' guide, or a bunch of other guides that might seem relevant to a newish user. $\endgroup$FreeQ[]
in the "Testing Expressions" is a most unfortunate omission. $\endgroup$Internal`DependsOnQ[expr,var]
which attempts (operative word, that) to determine if expr has a functional dependence on x. Can be useful for tasks where detecting literal symbol presence is not quite what is wanted. Example:In[1]:= Internal`DependsOnQ[f[x], x] Out[1]= True
$\endgroup$