Re: Pattern to match a list of non-negative integers
- To: mathgroup at smc.vnet.net
- Subject: [mg108942] Re: Pattern to match a list of non-negative integers
- From: dh <dh at metrohm.com>
- Date: Wed, 7 Apr 2010 07:27:20 -0400 (EDT)
- References: <hphbnd$lj5$1@smc.vnet.net>
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>