Re: Pattern to match a list of non-negative integers
- To: mathgroup at smc.vnet.net
- Subject: [mg108948] Re: Pattern to match a list of non-negative integers
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Thu, 8 Apr 2010 08:00:04 -0400 (EDT)
Hi Leo, Why not use {__Integer?NonNegative} Regards, Leonid On Wed, Apr 7, 2010 at 6:25 AM, Leo Alekseyev <dnquark at gmail.com> wrote: > I don't know why I didn't think about _?. How silly of me! > > Since I want to also enforce the Integer property, what I need is > MatchQ[{1, 2}, {_?(NonNegative[#] && Head[#] === Integer &) ..}] > > I am guessing this is the cleanest way to do it... > > > On Wed, Apr 7, 2010 at 6:07 AM, dh <dh 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:dh at metrohm.com> > > Internet:<http://www.metrohm.com> > > > > > >