Re: Strange ReplaceAll behavior
- To: mathgroup at smc.vnet.net
- Subject: [mg36835] Re: Strange ReplaceAll behavior
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sat, 28 Sep 2002 04:34:35 -0400 (EDT)
- References: <amuj8l$cf3$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hartmut, I add an explicit illustration to your ingenious solution using With. Hartmut's solution: Clear[g]; ru[a] = a -> x; With[{rule = ru[a], arg = x_}, g[arg] := a + b /. rule]; g[ c] b + c Why is arg = x_ needed? Without it we get Clear[g]; With[{rule=ru[a]}, g[x_]:=a+b/.rule]; g [c] b+x The reason for this shows in ?g Global`g g[x$_] := a + b /. a -> x The x in x_ has been changed to x$ and there is no x$ on the right side. This is a general feature of scoping. Taking it further we get Clear[g]; With[{rule=ru[a]},g[x_]:=a+x/.rule]; g [c] c+x ?g Global`g g[x$_] := a + x$ /. a -> x -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com> wrote in message news:amuj8l$cf3$1 at smc.vnet.net... > > >-----Original Message----- > >From: Lawrence A. Walker Jr. [mailto:lwalker701 at earthlink.net] To: mathgroup at smc.vnet.net > >Sent: Wednesday, September 25, 2002 7:51 AM > >Subject: [mg36835] Strange ReplaceAll behavior > > > > > >Hi, > > > >For the life of me I am not sure why the following is not > >working in my > >v. 4.2: > > > >ru[a]=a->x; > >f[x_]:=(a+b) /. ru[a]; > > > >Why do I get > >f[c] = b+x > > > >and not > >f[c] = b+c? > > > >What gives? > > > >Thanks, > >Lawrence > > > >-- > >Lawrence A. Walker Jr. > >http://www.kingshonor.com > > > > Lawrence, > > in your definition of f, x doesn't show up explicitely. So, in the > evaluation sequence, when the definition for f[c] is applied, no x appears > at rhs i.e. > (a + b) /. ru[a] and such c cannot be inserted. The result is the same as > directly executing > > In[11]:= (a + b) /. ru[a] > Out[11]= b + x > > If you don't like this, you have to make explicit the Value of ru[a] in the > definiton of f. One way to do so is to use Set instead of SetDelayed: > > In[9]:= f[x_] = (a + b) /. ru[a] > Out[9]= b + x > > In[10]:= f[c] > Out[10]= b + c > > The drawback of this that not only the value of ru[a] is inserted but also > the whole expression including ReplaceAll is evaluated. If this is not > wanted, you have to insert the value of ru[a] into the unevaluated rhs at > the definition. The general means for this are function application, With or > Replace: > > In[7]:= (g[x_] := (a + b) /. #) &[ru[a]] > In[8]:= g[c] > Out[8]= b + c > > In[16]:= Clear[g] > In[20]:= > Unevaluated[g[x_] := (a + b) /. rule] /. rule -> ru[a] > In[21]:= g[c] > Out[21]= b + c > > Here we have to prevent evaluation of the defintion before our rule is > inserted, this is achieved by Unevaluated. > > With is a bit more complicated, since the scoping rules for SetDelayed would > not allow the substition of an expression at rhs containing a pattern > variable (the pattern variable is renamed in this case). A simple answer to > this is to also substitute the argument variable (the pattern): > > > In[31]:= Clear[g] > In[32]:= > With[{rule = ru[a], arg = x_}, g[arg] := (a + b) /. rule] > In[33]:= g[c] > Out[33]= b + c > > -- > Hartmut Wolf > >