I would like to write a searcher like FreeQ that looks for a Symbol (or Head) in an expression, but confines its search outside of subexpressions that have a particular Head. Because expr can get huge, it is especially important to minimize the search time.

For example, I might want to test whether expr is free of Symbol=a outside of Head=func:

expr1 = a * b + c * func[a,x]

expr2 = b^2 + d * func[a,c]

Let's call this new searcher subFreeQ[expr_, symb_, head_]. Then I would like:

subFreeQ[expr1, a, func]

subFreeQ[expr2, a, func]

1 Answer 1


Just 'hiding' heads:

fq[ex_, a_, h_] := FreeQ[ex /. h[x__] :> Unique["x"], a],


fq[b^2 + f[a] + g[a, x] + h[b, x, a], , (f | g | h)]

yields True


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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