MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Localize Pattern variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107169] Re: Localize Pattern variables
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Thu, 4 Feb 2010 06:50:55 -0500 (EST)
  • References: <hkeb04$t7v$1@smc.vnet.net>

On 2010.02.04. 12:28, Istv=E1n wrote:
> 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?

Do you have a reasonable motive for not 'wanting' to use RuleDelayed?
This is the exact purpose of RuleDelayed.  It evaluates the rhs after,
not before the replacement.


  • Prev by Date: Re: Re: Numerical Problem
  • Next by Date: Re: Re: Re: What does & mean?
  • Previous by thread: Re: Localize Pattern variables
  • Next by thread: Re: Localize Pattern variables