Localize Pattern variables
- To: mathgroup at smc.vnet.net
- Subject: [mg107167] Localize Pattern variables
- From: István <replicatorzed at gmail.com>
- Date: Thu, 4 Feb 2010 06:29:08 -0500 (EST)
Dear Group, I often write small replacement codes inside (packaged) functions. One toy model is like this: In[78]:= func[x_List] := ReplaceAll[x, r_Integer -> r + 100]; r =.; func@{1, 2, 3, 4} Out[80]= {101, 102, 103, 104} In[81]:= r = -100; func@{1, 2, 3, 4} Out[82]= {0, 0, 0, 0} What is the best way to localize r in func? Obviously modularizing it won't solve the matter: func[x_List] := Module[{r}, ReplaceAll[x, r_Integer -> r + 100]]; produces the very same output. I can also use Block, instead of Module, which seemingly works, but then consider this version, where r interferes with an in-Block variable of the same name: func[x_List] := Block[{r}, r = 12; ReplaceAll[x, r_Integer -> r + 100]]; Of course I can always use RuleDelayed, but what if I don't want to. The embarrassing thing is that I'm pretty sure that before code colouring (v6), I missed this intereference quite a few times, assuming that lhs pattern variables are automatically bound with rhs ones, with a higher priority than assigning the value of existing variables of the same name to rhs pattern variables. How can I restrict (i.e. localize) non-delayed pattern variables to rules? Why RuleDelayed makes r local but not Rule? Am I missing something? Is there a general way to deal with this matter? Thanks Istvan
- Follow-Ups:
- Re: Localize Pattern variables
- From: Leonid Shifrin <lshifr@gmail.com>
- Re: Localize Pattern variables