Re: Pattern to match a list of non-negative integers
- To: mathgroup at smc.vnet.net
- Subject: [mg108970] Re: Pattern to match a list of non-negative integers
- From: "Carl K. Woll" <carlw at wolfram.com>
- Date: Thu, 8 Apr 2010 08:04:09 -0400 (EDT)
On 4/7/2010 3:20 AM, Leo Alekseyev wrote: > This seems like a very basic pattern-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 to 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. > >