Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Pattern to match a list of non-negative integers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg108932] Re: Pattern to match a list of non-negative integers
  • From: Leo Alekseyev <dnquark at gmail.com>
  • Date: Wed, 7 Apr 2010 07:25:30 -0400 (EDT)
  • References: <hphbnd$lj5$1@smc.vnet.net> <4BBC5963.3010105@metrohm.com>

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>
>
>


  • Prev by Date: Re: Show a maximum curve in ListContourPlot
  • Next by Date: Re: Mathematica Programming
  • Previous by thread: Re: Pattern to match a list of non-negative integers
  • Next by thread: Re: Pattern to match a list of non-negative integers