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: [mg108934] Re: Pattern to match a list of non-negative integers
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Wed, 7 Apr 2010 07:25:52 -0400 (EDT)
  • References: <hphbnd$lj5$1@smc.vnet.net>

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


  • Prev by Date: Re: Intel MKL 10
  • Next by Date: Re: Pattern to match a list of non-negative integers
  • Previous by thread: Pattern to match a list of non-negative integers
  • Next by thread: Re: Pattern to match a list of non-negative integers