MemberQ


The first example is simple. MemberQ finds that the sum includes (x) to an
Integer power.

Clear[x, y] ;  MemberQ[1 + x + x^2, Power[x, _Integer]]

True


You might wonder why MemberQ doesn't return True in this case.  The reason is
that the default level specification doesn't have MemberQ check the whole
expression.

MemberQ[x^2, Power[x, _Integer]]

False

Next MemberQ is given a level specification that has it check all levels including the whole expression, and True is  returned.

MemberQ[x^2, Power[x, _Integer], {0, -1}]

True

Next the default level specification stops MemberQ from looking deep  enough to find the x^2.

MemberQ[{1 + x + x^2, y}, Power[x, _Integer]]

False

In the next example MemberQ is given a level specification deep enough to  find the x^2.

MemberQ[{1 + x + x^2, y}, Power[x, _Integer], {0, -1}]

True

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

Heads Option


MemberQ has a Heads option with the default setting (Heads→False).  So
in the next example MemberQ doesn't check the heads of subexpressions.

MemberQ[1 + x + x^2, Plus]

False


In the next example the setting (Heads→True) is used and MemberQ does
check the heads of subexpressions.

MemberQ[1 + x + x^2, Plus, HeadsTrue]

True


Created by Mathematica  (May 16, 2004)