I have a complicated expression in which I have another expression p which, when it’s present, I want to match the outer expression and all of its children. I was hoping for something like Descendant[p] which matches p if it appears anywhere in the child tree of the expression. For example:

X = ElementA[a,b,c,


MatchQ[X, ElementA[Descendant[foo]]]

Would match the entire expression ElementA[...] because foo appears somewhere as a descendant.

  • 2
    $\begingroup$ Something like MatchQ[X, e : ElementA[args__] /; ! FreeQ[e, foo]]? $\endgroup$ – J. M. will be back soon Mar 14 at 2:59
  • 2
    $\begingroup$ ... or MatchQ[X, _ElementA?(Not@*FreeQ[foo])] $\endgroup$ – kglr Mar 14 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.