Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Roger Wilson

Posts: 16
Registered: 10/23/10
Re: Scoping of pattern names
Posted: Nov 8, 2011 6:31 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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
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.




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.