Re: Pattern to match a list of non-negative integers

• To: mathgroup at smc.vnet.net
• Subject: [mg109009] Re: Pattern to match a list of non-negative integers
• From: Raffy <adraffy at gmail.com>
• Date: Sat, 10 Apr 2010 06:53:15 -0400 (EDT)
• References: <hpkgmo\$83k\$1@smc.vnet.net>

```On Apr 8, 5:03 am, "Carl K. Woll" <ca... at wolfram.com> wrote:
> On 4/7/2010 3:20 AM, Leo Alekseyev wrote:> This seems like a very basic p=
attern-matching question, but somehow
> > the answer eludes me at the moment.  I want to match a list of
> > non-negative integers.  Something like MatchQ[{1,2},{(x_Integer /; x
>
> >> = 0)..}] doesn't work -- do named patterns simply not play well with
>
> > Repeated[]?..
>
> Yes. The named pattern must be the same, so only {1,1} would match.
> Instead you can use PatternTest:
>
> MatchQ[{1,2}, {_Integer?(#!=0&) .. }]
>
> Of course, simpler is to use:
>
> MatchQ[{1,2}, {__Integer?NonNegative}]
>
> Carl Woll
> Wolfram Research
>
>
>
> > After starting to write this message, the following pattern, occurred t=
o me:
> > MatchQ[{2, 3}, x : {_Integer ..} /; ! MemberQ[x, y_ /; Negative[y]]]
> > -- this works, but seems needlessly complex -- so I'll send the
> > message on, in hopes that there is a cleaner way of writing the
> > pattern.

I was surprised that __?Test works properly by applying the test to
each element of the matched sequence.

I would not of expected this behavior, however, after looking, it does
appear in the documentation.

Thanks.

```

• Prev by Date: Re: List Manipulation: Conditionally changing y value when x value meets some criteria
• Next by Date: Simultaneous Nonlinear Data Fits
• Previous by thread: Re: Pattern to match a list of non-negative integers
• Next by thread: Collecting like terms after TrigReduce