[Date Index] [Thread Index] [Author Index]
Re: Avoid iteration in pattern test?
You could try: x /: HoldPattern[Times][f__, x] /; ! FreeQ[Hold[f], d] := "matched" Carl Woll On 1/6/2014 1:30 AM, Yi Wang wrote: > Dear all, > > I wonder if one can stop pattern test from unnecessary iteration. For example > > ClearAll[x, a, b, c, d, f] > x /: f_ x := "matched" /; !FreeQ[f, d] > > Trace[a b c x] > > One can find that Mathematica first tests if a*b*c is free of d. Then it tests if a*b is free of d, if a*c is free of d, ... > > However, we know that if a*b*c is free of d, all the sub-sequences are free of d. Thus is it possible to rewrite the above rule, to stop Mathematica from wasting the efforts? > > Thanks a lot! > Yi > > PS: currently, I have a workaround -- to hold evolution of x when no match. However, it makes things complicated and less automatic. > > x /: f_ x := If[!FreeQ[f, d], "matched", f Hold[x]] >