Re: Pure Functions in rules
- To: mathgroup at smc.vnet.net
- Subject: [mg16014] Re: [mg15932] Pure Functions in rules
- From: Clemens Frey <btml01 at uni-bayreuth.de>
- Date: Sat, 20 Feb 1999 02:52:05 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Hi Will! The problem in your In/Out[80] is that you employ Rule instead of RuleDelayed; but you need evaluation after pattern matching, so just analogously to using SetDelayed instead of Set when defining a function you have to use RuleDelayed, and it will work as intended: In[81] := {1,2,3}/.(m_List:>(2*#& /@ m)) Out[80] = {2,4,6} Hope this helped Clemens ------------------------------------------------------------ Clemens Frey Doctoral Student at the Department of Mathematics/BITOEK University of Bayreuth(Germany) clemens.frey at uni-bayreuth.de http://www.bitoek.uni-bayreuth.de/~Clemens.Frey ------------------------------------------------------------ On Wed, 17 Feb 1999, 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 >