Re: Pattern to match a list of non-negative integers
- To: mathgroup at smc.vnet.net
- Subject: [mg108944] Re: Pattern to match a list of non-negative integers
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Thu, 8 Apr 2010 07:59:19 -0400 (EDT)
MatchQ[{2, 3},
x : {_Integer?NonNegative ..}]
True
MatchQ[{2, 3},
x : {_?(IntegerQ[#] && # > 0 &) ..}]
True
Bob Hanlon
---- Leo Alekseyev <dnquark at gmail.com> 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[]?..
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.