Avoid iteration in pattern test?
- To: mathgroup at smc.vnet.net
- Subject: [mg132157] Avoid iteration in pattern test?
- From: Yi Wang <tririverwangyi at gmail.com>
- Date: Mon, 6 Jan 2014 02:30:34 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
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]]
- Follow-Ups:
- Re: Avoid iteration in pattern test?
- From: Carl K Woll <carlw@wolfram.com>
- Re: Avoid iteration in pattern test?