Re: Pattern to match a list of non-negative integers
- To: mathgroup at smc.vnet.net
- Subject: [mg108955] Re: Pattern to match a list of non-negative integers
- From: Raffy <adraffy at gmail.com>
- Date: Thu, 8 Apr 2010 08:01:22 -0400 (EDT)
- References: <hphbnd$lj5$1@smc.vnet.net> <hphq6g$1i9$1@smc.vnet.net>
On Apr 7, 4:27 am, dh <d... at metrohm.com> wrote: > On 07.04.2010 09:20, 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[]?.. > > > 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. > > Hi Leo, > if you repeat something like x_ .., you repeat the x, that is all the > elements must be the same. Therefore, do not name the pattern. The > following will work: > MatchQ[{1, 2}, {_ ?NonNegative ..}] > > cheers, Daniel > > -- > > Daniel Huber > Metrohm Ltd. > Oberdorfstr. 68 > CH-9100 Herisau > Tel. +41 71 353 8585, Fax +41 71 353 8907 > E-Mail:<mailto:d... at metrohm.com> > Internet:<http://www.metrohm.com> MatchQ[ {1,2}, {_Integer?NonNegative..} ] MatchQ[ {1,2}, {x__Integer /; NonNegative@Min[x]} ]