RE: ReplaceList -- Unexpected Answer
- To: mathgroup at smc.vnet.net
- Subject: [mg46808] RE: [mg46788] ReplaceList -- Unexpected Answer
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 9 Mar 2004 04:30:51 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Harold.Noffke at wpafb.af.mil [mailto:Harold.Noffke at wpafb.af.mil] To: mathgroup at smc.vnet.net >Sent: Monday, March 08, 2004 10:10 AM >To: mathgroup at smc.vnet.net >Subject: [mg46808] [mg46788] ReplaceList -- Unexpected Answer > > >MathGroup: > >In The Mathematica 5 Book, Section 2.3.3 Naming Pieces of Patterns, we >find the following pattern matching exercise ... > > Now both arguments of f are constrained to be the same, >and only the > first case matches. > > In[5]:= > {f[h[4], h[4]], f[h[4], h[5]]} /. f[x:h[_], x_] -> r[x] > Out[5]= > {r[h[4]],f[h[4],h[5]]} > > >Now, let's use ReplaceList to get more insight into this matching >process ... > > In[6]:= > ReplaceList[ >{f[h[4],h[4]],f[h[4],h[5]]},f[x:h[_],x_] -> r[x] ] > Out[6]= > {} > >I do not understand why ReplaceList returns {} instead of { r[h[4]] }. > >Regards, >Harold > Harold, from Help: "ReplaceList[expr, rules] attempts to transform the entire expression expr by applying a rule or list of rules in all possible ways, and returns a list of the results obtained." That is: the pattern from the rules have to match the entire expression. Such try In[2]:= ReplaceList[{f[h[4], h[4]], f[h[4], h[5]]}, {a___, f[x : h[_], x_], o___} -> {a, r[x], o}] Out[2]= {{r[h[4]], f[h[4], h[5]]}} You get only one match! This gives two matches: In[3]:= ReplaceList[{f[h[4], h[4]], f[h[5], h[5]]}, {a___, f[x : h[_], x_], o___} -> {a, r[x], o}] Out[3]= {{r[h[4]], f[h[5], h[5]]}, {f[h[4], h[4]], r[h[5]]}} -- Hartmut