From: Leo Alekseyev on
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.

From: Leo Alekseyev on
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(a)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(a)metrohm.com>
> Internet:<http://www.metrohm.com>
>
>

From: David Bailey on
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.
>
Well probably the neatest solution is to write a test suitableListQ that
checks the list out and returns True or False, and then write:

x_?suitableListQ

This is of course, extremely general.

BTW, In time critical code, I tend to avoid complex patterns, because I
find their performance can be a bit unpredictable.

David Bailey
http://www.dbaileyconsultancy.co.uk

From: dh on
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(a)metrohm.com>
Internet:<http://www.metrohm.com>


From: Bob Hanlon on

MatchQ[{2, 3},
x : {_Integer?NonNegative ..}]

True

MatchQ[{2, 3},
x : {_?(IntegerQ[#] && # > 0 &) ..}]

True


Bob Hanlon

---- Leo Alekseyev <dnquark(a)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.