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:= MatchQ[f[1, 2], f[l_Integer, r___]] > Out= True > > In:= SetAttributes[f, Flat] > > In:= MatchQ[f[1, 2], f[l_Integer, r___]] > Out=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:=$VersionNumber > Out=4. > > In:=$ReleaseNumber > Out=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:= MatchQ[f[1, 2], f[l_, r_]] Out= True In:= MatchQ[f[1, 2], f[l_, r___]] Out= True But MatchQ isn't so well suited for studying. Let's better try: In:= f[1, 2] /. f[l_, r_] -> g[l, r] Out= g[f, f] In:= f[1, 2] /. f[l_, r___] -> g[l, r] Out= g[f, 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:= f[1, 2] /. f[l_] -> g[l] Out= g[f[1, 2]] ...and also what has been done at Out! If you, in addition give f the attribute OneIdentity In:= SetAttributes[f, OneIdentity] ...you may get more intuitive results. Now the single argument a will not be converted to f[a]. In:= f[1, 2] /. f[l_, r_] -> g[l, r] Out= g[1, 2] In:= f[1, 2] /. f[l_, r___] -> g[l, r] Out= g[1, 2] And of course you now get a match In:= MatchQ[f[1, 2], f[l_Integer, r___] ] Out= 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