Re: Pure Functions in rules
- To: mathgroup at smc.vnet.net
- Subject: [mg15981] Re: Pure Functions in rules
- From: David Reiss <David_B_Reiss_NoSpam at res.raytheon.com>
- Date: Fri, 19 Feb 1999 03:27:02 -0500
- Organization: Raytheon Company
- References: <7ag34l$aie@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Will, This has to do with delayed versus immediate replacement. In[1]:= {1,2,3}/.(m_List->(2*#& /@ m)) Out[1]= {1,2,3} In[2]:= {1,2,3}/.(m_List:>(2*#& /@ m)) Out[2]= {2,4,6} For In[1] above, the right hand side or the replacement rule ((2*#& /@ m)) is evaluated before the rule is applied the result of evaluating (2*#& /@ m) is just m, so {1,2,3}/.(m_List->(2*#& /@ m)) is equivilant to {1,2,3}/.(m_List-> m) as long as m hasn't been defined previously in the Mathematica session. These are the same issues as arise for "=" versus ":=" In In[2] I use RuleDelayed and the result that you expect is obtained since the right hand side is not evaluated prior to the execution of the rule. Cheers, David Will Self wrote: > It appears that I cannot depend on using a pure function > in a pattern-matching rule. > > Here I am trying to convince reluctant students that they're > better off learning to use Mathematica than doing things > by hand, and we run across something like this, and in a > much more complicated situation where the trouble was > hard to isolate. > > I am quite frankly incensed by the behavior shown in > In/Out 80, below. Look at these examples: > > In[73]:= {1,2,3}/.(m_List->7) > Out[73]= 7 > > In[74]:= {1,2,3}/.(m_List->(2*m)) > Out[74]= {2,4,6} > > In[75]:= 2*#& /@ {1,2,3} > Out[75]= {2,4,6} > > In[77]:= f[m_List]:=2*#& /@ m > > In[78]:= f[{1,2,3}] > Out[78]= {2,4,6} > > In[79]:= {1,2,3}/.m_List->f[m] > Out[79]= {2,4,6} > > Now try this: > > In[80]:= {1,2,3}/.(m_List->(2*#& /@ m)) > Out[80]= {1,2,3} > > Does anyone (say, at WRI for example) care to comment on > this? > > Will Self