FreeQ

FreeQ is like MemberQ in that is also takes a level specification.  By default FreeQ looks at all levels, so in the next example FreeQ returns False because it see the 2 in x^2.

FreeQ[y + x + x^2, _Integer]

False

Next I tell FreeQ to only look at level 1 and it says there are no Integers.

FreeQ[y + x + x^2, _Integer, 1]

True

Next FreeQ returns False because the whole expression matches the pattern (_List).

FreeQ[{1, 0}, _List]

False

I don't provide further examples, but any combination of pattern matching constructs can be used with FreeQ.  Nuances of pattern matching are discussed in another section.

Heads Option

FreeQ has a Heads option like several other functions, and it has the default setting (Heads→True).  So in the next example FreeQ checks the heads of subexpressions and determines that there is one with the head Plus.

FreeQ[{{x, y}, {1 + x + x^2, y}}, Plus]

False

In the next example the setting (Heads→False) is used and FreeQ doesn't check the heads of subexpressions.

FreeQ[{{x, y}, {1 + x + x^2, y}}, Plus, HeadsFalse]

True


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page