Re: Pattern Matcher

*To*: mathgroup at christensen.cybernetics.net*Subject*: [mg541] Re: [mg535] Pattern Matcher*From*: withoff (David Withoff)*Date*: Mon, 13 Mar 1995 18:03:20 -0600

> Hi! > > Does anyone know how Mathematica's pattern matcher works if > expressions to be matched and/or pattern involve functions > having attribute Orderless? Does it simply try all cases one by one? > Or maybe it makes something more intellectual? Any references? > > Regards, > Sergei Klioner > Senior Scientist > Intitute of Apllied Astronomy, > St.Petersburg, Russia There are examples in section 2.3 of the Mathematica book for seeing the combinations that are tried by the pattern matcher. You can use the same tricks to analyze your own examples and find out what the pattern matcher is doing. Here is an example. In[1]:= SetAttributes[f, Orderless] In[2]:= f[a, b, c, d] /. f[p_, q_, r_, s_] :> Null /; Print[{p, q, r, s}] {a, b, c, d} {a, b, d, c} {a, c, b, d} {a, c, d, b} {a, d, b, c} {a, d, c, b} {b, a, c, d} {b, a, d, c} {b, c, a, d} {b, c, d, a} {b, d, a, c} {b, d, c, a} {c, a, b, d} {c, a, d, b} {c, b, a, d} {c, b, d, a} {c, d, a, b} {c, d, b, a} {d, a, b, c} {d, a, c, b} {d, b, a, c} {d, b, c, a} {d, c, a, b} {d, c, b, a} Out[2]= f[a, b, c, d] In some examples it is possible to eliminate cases without checking the condition. In other examples the only practical thing to do, and in many examples the only thing to do, is to try all cases one by one. Dave Withoff Research and Development Wolfram Research