I have been thinking how MemberQ
is a sort of special case of MatchQ
. MemberQ
assumes a list, rather than simply a pattern, as MatchQ
does -- as its second argument. For example:
MemberQ[{a, b}, a]
MatchQ[{a, b}, {___, a, ___}]
MatchQ[{a, b}, {___, ___, a, ___, ___}]
MatchQ[{a, b}, List[___, a, ___]]
MatchQ[{a, b}, List[___, ___, a, ___, ___]]
(* OUTPUT: *)
(* True *)
(* True *)
(* True *)
(* True *)
(* True *)
Is there any way to write the pattern in MatchQ
without explicitly specifying the List
? I'm thinking of something like the following: MatchQ[{a, b}, ___ ~~ a ~~ ___]
...but that's incorrect syntax (and returns False
) because the use of ~~
implies a StringExpression
, and {a, b}
is clearly not a StringExpression
. So, how can I write a pattern representing "any subexpression containing the symbol a
anywhere"?
I could accomplish this using the resource function ContainsQ
:
ResourceFunction["ContainsQ"][{a, b}, a]
(* OUTPUT: *)
(* True *)
But what is the built-in way to do this?
Looking at the source notebook for the resource function ContainsQ
, I see that the definition of ContainsQ
is simply:
ContainsQ[args___] := (Needs["GeneralUtilities`"]; GeneralUtilities`ContainsQ[args])