MemberQ
MemberQ[expr, pat]
returns True if any of the elements in expr
match pat
, otherwise returns False.
Attributes[MemberQ] := {Protected}
Simple examples
In[1]:= MemberQ[{1, 2, 3}, 0]
Out[1]= False
In[2]:= MemberQ[{1, 2, 3}, 1]
Out[2]= True
In[3]:= MemberQ[{1, 2, 3}, {1}]
Out[3]= False
MemberQ
works with patterns:
In[4]:= MemberQ[{1, 2, 3}, _Integer]
Out[4]= True
In[5]:= MemberQ[{1, 2, 3}, _]
Out[5]= True
In[6]:= MemberQ[{1, 2, 3}, _Real]
Out[6]= False
In[7]:= MemberQ[{1, 2, 3}, testmatch_Integer]
Out[7]= True
In[8]:= testmatch
Out[8]= testmatch
In[9]:= MemberQ[a, a]
Out[9]= False
In[10]:= MemberQ[a, _]
Out[10]= False
In[11]:= MemberQ[{a, b}, c]
Out[11]= False
In[12]:= MemberQ[{a, b}, a]
Out[12]= True
Further examples
MemberQ
works with BlankSequences:
In[1]:= MemberQ[{a, b}, ___]
Out[1]= True
In[2]:= MemberQ[{a, b}, __]
Out[2]= True
In[3]:= MemberQ[{a, b}, __Integer]
Out[3]= False
In[4]:= MemberQ[{a, b}, ___Integer]
Out[4]= False
In[5]:= MemberQ[{a, b}, ___Symbol]
Out[5]= True
In[6]:= MemberQ[{a, b}, __Symbol]
Out[6]= True
In[7]:= MemberQ[{a, b, 1}, __Symbol]
Out[7]= True
In[8]:= MemberQ[{a, b, 1}, __Integer]
Out[8]= True
expr
need not be a List:
In[9]:= MemberQ[bar[a, b, c], a]
Out[9]= True
In[10]:= MemberQ[bar[a, b, c], bar]
Out[10]= False