Re: Inconsistencies in pattern matching.

*To*: mathgroup at smc.vnet.net*Subject*: [mg12992] Re: [mg12937] Inconsistencies in pattern matching.*From*: "Jrgen Tischer" <jtischer at col2.telecom.com.co>*Date*: Sun, 28 Jun 1998 02:52:14 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Ok Sean, let's try to convince you of the unthinkable. First the "definition" of ReplaceAll (Book, online help on ReplaceAll): ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr. The first rule that applies to a particular part is used; no further rules are tried on that part, or on any of its subparts. What it says is that ReplaceAll is traversing the tree structure of the expression breadth first. What you don't like so much is that Mathematica starts at the root. But this IS very consistent. How should Mathematica know that in the case of {x1,y1}/.{x_,y_}->{x,Log[y]} you want it start at the root and in the case of {{x1,y1},{x2,y2}}/.{x_,y_}->{x,Log[y]} not? By the way this is what restrictions as you used one are good for. Satisfied? Jrgen -----Original Message----- From: Sean Ross <seanross at worldnet.att.net> To: mathgroup at smc.vnet.net Subject: [mg12992] [mg12937] Inconsistencies in pattern matching. >This is an example taken from Ken Wagners Power Programming book: > >In[1]:={{x1,y1},{x2,y2},{x3,y3}}/.{x_,y_}->{x,Log[y]} > >Out[1]:={{x1,Log[y1]},{x2,Log[y2]},{x3,Log[y3]}} > >In[2]:={{x1,y1}}/.{x_,y_}->{x,Log[y]} > >Out[2]:={{x1,Log[y1]}} > >As long as the list of {x,y} data points has one point or greater than >two points, it transforms as one would expect. If there are two {x,y} >data points, it transforms differently. > >In[3]:={{x1,y1},{x2,y2}}/.{x_,y_}->{x,Log[y]} > >Out[3]:={{x1,y1},{Log[x2],Log[y2]}} > > >Now, if x_ is seen to match {x1,y1} in example 3, then why doesn't it >also match it in example number 1 and 2? This behavior can be fixed >with a /;Head[x]=!=List pattern restricting rule, but that is not the >point. This seems grossly inconsistent to me. Can anyone explain why >it does this and/or justify that this is a good thing? >