Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2757 > unrolled thread

Re: Problem combining patterns

Started byLeonid Shifrin <lshifr@gmail.com>
First post2011-05-28 11:19 +0000
Last post2011-05-29 11:36 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Problem combining patterns Leonid Shifrin <lshifr@gmail.com> - 2011-05-28 11:19 +0000
    Re: Problem combining patterns David Bailey <dave@removedbailey.co.uk> - 2011-05-29 11:36 +0000

#2757 — Re: Problem combining patterns

FromLeonid Shifrin <lshifr@gmail.com>
Date2011-05-28 11:19 +0000
SubjectRe: Problem combining patterns
Message-ID<irqlng$e7i$1@smc.vnet.net>
You can do

ClassPattern[] := _?(Length[#] === 1 &);

or

ClassPattern[] := _ [ _ ]

Regards,
Leonid


On Fri, May 27, 2011 at 2:12 PM, Rui <rui.rojo@gmail.com> wrote:

> Hey people.
> I want to have an expression that evaluates to a pattern. Imagine
> something simple
>
> ClassPattern[]:=HoldPattern[i_/;Length[i]===1];
> So far so good. But then I'd like to use it in compound expressions
> like
>
> MatchQ[{b[3], c[5]}, {ClassPattern[], ClassPattern[]}]
>
> When I do so, the label "i" inside the pattern is treated as the same
> in both ClassPatterns, so I get false. But I wanna get True in cases
> like the one before
>
> Any ideas?
>
>

[toc] | [next] | [standalone]


#2786

FromDavid Bailey <dave@removedbailey.co.uk>
Date2011-05-29 11:36 +0000
Message-ID<irtb4o$pjk$1@smc.vnet.net>
In reply to#2757
On 28/05/2011 12:19, Leonid Shifrin wrote:
> You can do
>
> ClassPattern[] := _?(Length[#] === 1&);
>
> or
>
> ClassPattern[] := _ [ _ ]
>
> Regards,
> Leonid
>
>
> On Fri, May 27, 2011 at 2:12 PM, Rui<rui.rojo@gmail.com>  wrote:
>
>> Hey people.
>> I want to have an expression that evaluates to a pattern. Imagine
>> something simple
>>
>> ClassPattern[]:=HoldPattern[i_/;Length[i]===1];
>> So far so good. But then I'd like to use it in compound expressions
>> like
>>
>> MatchQ[{b[3], c[5]}, {ClassPattern[], ClassPattern[]}]
>>
>> When I do so, the label "i" inside the pattern is treated as the same
>> in both ClassPatterns, so I get false. But I wanna get True in cases
>> like the one before
>>
>> Any ideas?
>>
>>
I think that ClassPattern may be a cut down example, and naming the 
pattern variables may be useful in the real problem, so how about:

ClassPattern[] :=Module[{pp}, HoldPattern[i_ /; Length[i] === 1] /. i -> pp]

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web