Prev: Show a maximum curve in ListContourPlot
Next: Different results with ReplaceAll and direct substitution
From: Leonid Shifrin on 8 Apr 2010 07:59 Hi Leo, Why not use {__Integer?NonNegative} Regards, Leonid On Wed, Apr 7, 2010 at 6:25 AM, Leo Alekseyev <dnquark(a)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(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: Raffy on 8 Apr 2010 08:00 On Apr 7, 4:27 am, dh <d...(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:d...(a)metrohm.com> > Internet:<http://www.metrohm.com> MatchQ[ {1,2}, {_Integer?NonNegative..} ] MatchQ[ {1,2}, {x__Integer /; NonNegative(a)Min[x]} ]
From: Carl K. Woll on 8 Apr 2010 08:03 On 4/7/2010 3:20 AM, 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[]?.. > > Yes. The named pattern must be the same, so only {1,1} would match. Instead you can use PatternTest: MatchQ[{1,2}, {_Integer?(#!=0&) .. }] Of course, simpler is to use: MatchQ[{1,2}, {__Integer?NonNegative}] Carl Woll Wolfram Research > 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: Raffy on 10 Apr 2010 06:53 On Apr 8, 5:03 am, "Carl K. Woll" <ca...(a)wolfram.com> wrote: > On 4/7/2010 3:20 AM, Leo Alekseyev wrote:> This seems like a very basic p= attern-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[]?.. > > Yes. The named pattern must be the same, so only {1,1} would match. > Instead you can use PatternTest: > > MatchQ[{1,2}, {_Integer?(#!=0&) .. }] > > Of course, simpler is to use: > > MatchQ[{1,2}, {__Integer?NonNegative}] > > Carl Woll > Wolfram Research > > > > > After starting to write this message, the following pattern, occurred t= o 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. I was surprised that __?Test works properly by applying the test to each element of the matched sequence. I would not of expected this behavior, however, after looking, it does appear in the documentation. Thanks.
First
|
Prev
|
Pages: 1 2 Prev: Show a maximum curve in ListContourPlot Next: Different results with ReplaceAll and direct substitution |