Re: Scoping of pattern names
- To: mathgroup at smc.vnet.net
- Subject: [mg122744] Re: Scoping of pattern names
- From: Roger Wilson <rogerhw999 at gmail.com>
- Date: Tue, 8 Nov 2011 07:17:32 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j98djh$3r0$1@smc.vnet.net>
On Nov 7, 10:57 am, Rui <rui.r... at 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 your problem correctly)... 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.