AW: Matching on Arguments of Flat Functions ...
- To: mathgroup at smc.vnet.net
- Subject: [mg29591] AW: [mg29569] Matching on Arguments of Flat Functions ...
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
- Date: Wed, 27 Jun 2001 05:12:26 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Please see below > -----Ursprüngliche Nachricht----- > Von: Detlef Mueller [mailto:dmueller at mathematik.uni-kassel.de] > Gesendet: Dienstag, 26. Juni 2001 02:42 > An: mathgroup at smc.vnet.net > Betreff: [mg29569] Matching on Arguments of Flat Functions ... > > > Little Session: > > " > In[1]:= MatchQ[f[1, 2], f[l_Integer, r___]] > Out[1]= True > > In[2]:= SetAttributes[f, Flat] > > In[3]:= MatchQ[f[1, 2], f[l_Integer, r___]] > Out[3]=False > " > > Oops! What happens here? > I heard, that this Attribute may influence > Matching - but I don't see, why something > matched before now doesnt match anymore. > > Btw: > > " > In[5]:=$VersionNumber > Out[5]=4. > > In[6]:=$ReleaseNumber > Out[6]=1 > " > > Greetings > Detlef > Detlef, matching functions with attribute Flat indeed does not meet our innocent expectations. So it is indispensable to carefully read what S.Wolfram has written (§2.3.7). You will find In[8]:= MatchQ[f[1, 2], f[l_, r_]] Out[8]= True In[9]:= MatchQ[f[1, 2], f[l_, r___]] Out[9]= True But MatchQ isn't so well suited for studying. Let's better try: In[14]:= f[1, 2] /. f[l_, r_] -> g[l, r] Out[14]= g[f[1], f[2]] In[15]:= f[1, 2] /. f[l_, r___] -> g[l, r] Out[15]= g[f[1], 2] Since for purpose of matching, the argument is wrapped with f (un-flattend so to speak), it can't match l_Integer! Also matching of sequences is done in a special way. Regard In[20]:= f[1, 2] /. f[l_] -> g[l] Out[20]= g[f[1, 2]] ...and also what has been done at Out[15]! If you, in addition give f the attribute OneIdentity In[21]:= SetAttributes[f, OneIdentity] ...you may get more intuitive results. Now the single argument a will not be converted to f[a]. In[22]:= f[1, 2] /. f[l_, r_] -> g[l, r] Out[22]= g[1, 2] In[23]:= f[1, 2] /. f[l_, r___] -> g[l, r] Out[23]= g[1, 2] And of course you now get a match In[25]:= MatchQ[f[1, 2], f[l_Integer, r___] ] Out[25]= True As you may suspect, matching with Flat attribute is a recurrent theme. So you certainly will search the archive for more and better answers. -- Hartmut Wolf