Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Scoping of pattern names
Replies: 1   Last Post: Nov 8, 2011 6:31 AM

 Messages: [ Previous | Next ]
 Roger Wilson Posts: 16 Registered: 10/23/10
Re: Scoping of pattern names
Posted: Nov 8, 2011 6:31 AM

On Nov 7, 10:57 am, Rui <rui.r...@gmail.com> wrote:
> My goal:
> store a pattern in a variable for future use
>
> My problem:
> if the pattern I stored has a pattern name in it, (like i_/;i=8),
> and I use it later to build a new pattern in which I use the stored pattern twice, it thinks its the same name (i)
>
> My investigation:
> So far, couldn't find any way to scope pattern's names in a useful way.
> Module and With, refuse to scope a pattern name that's somewhere on the left hand side of a condition contained in them.
>
> Sure, I probably haven't tried much yet to build what I want with hard expression manipulation, but... I found my objective neat simple and useful...
> I would like to know if there's a proper way to do this, or to achieve my objective somehow else. And, I'd also enjoy knowing why such a resistance to scope pattern names...

I would suggest something along the lines of this (if I understand

11/7/11 13:09:43 In[56]:= Clear[p]
11/7/11 13:09:44 In[57]:= p[]:= Module[{i,z},z=Condition[Apply[Pattern,
{i,Blank[]}],Or[Equal[i,8],Equal[i,9]]]; z];
11/7/11 13:09:44 In[58]:= p[]
11/7/11 13:09:44 Out[58]= i\$682_/;i\$682==8||i\$682==9
11/7/11 13:09:45 In[59]:= MatchQ[1,p[]]
11/7/11 13:09:45 Out[59]= False
11/7/11 13:09:46 In[60]:= MatchQ[8,p[]]
11/7/11 13:09:46 Out[60]= True
11/7/11 13:09:46 In[61]:= MatchQ[{8,9},{p[],p[]}]
11/7/11 13:09:46 Out[61]= True

The Apply prevents the Pattern being evaluated when the Module is
defined so that when p[] is called a new local temporary i is created
each time so two successive calls (as in the last example) create two
different patterns and so can match to 8,9 rather than only 8,8 and
9,9.

Date Subject Author
11/7/11 Rui
11/8/11 Roger Wilson