       How to specify "arbitrary depth" for a (sub)pattern?

• To: mathgroup at smc.vnet.net
• Subject: [mg113944] How to specify "arbitrary depth" for a (sub)pattern?
• From: kj <no.email at please.post>
• Date: Fri, 19 Nov 2010 05:07:16 -0500 (EST)

```Given some pattern P, what's the pattern Q[P] such that
MatchQ[X, Q[P]] will evaluate to True if and only if either X or
any of its subexpressions match P?

Here's a common situation where I want this sort of pattern Q[P].

Suppose that I have a list L, defined like this (for example):

In:= L = {1, a + b, 2, (a + b)^2, 3, (a + b)^2/c, 4, Sin[(a + b)^2/c]};

...and I want to extract from L all those members that contain (at
any depth) an expression whose head is Plus.  I.e., I want to get
the list

{a + b, (a + b)^2, (a + b)^2/c, Sin[(a + b)^2/c}

This seems like a job for Cases, but I don't know how to specify the
right pattern for it to get the desired result:

In:= Cases[L, _Plus]
Out= {a + b}

Passing a levelspec to Cases won't do the trick:

In:= Cases[L, _Plus, Infinity]
Out= {a + b, a + b, a + b, a + b}

In other words, what I really need here is Cases[L, Q[_Plus]], but
I don't know how to specify it.

TIA!

~kj

```

• Prev by Date: Re: Mathematica 8 "natural language" capabilities
• Next by Date: Re: Mathematica 8 "natural language" capabilities
• Previous by thread: Re: Sizing opening window in Mathematica 8
• Next by thread: Re: How to specify "arbitrary depth" for a (sub)pattern?