Re: Deleting expressions containing a certain head, and a plea for Esc-fn-Esc

*To*: mathgroup at smc.vnet.net*Subject*: [mg126068] Re: Deleting expressions containing a certain head, and a plea for Esc-fn-Esc*From*: Andrzej Kozlowski <akozlowski at gmail.com>*Date*: Sat, 14 Apr 2012 03:12:33 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <201204130857.EAA19519@smc.vnet.net>

What causes the problem is the high precedence of PatternTest, which is explained in the documentation: The operator ? has a high precedence. Thus _^_?t is _^(_?t), not (_^_)?t For the same reason _?(FreeQ [#, Complex ]) & is not a PatternTest but a Function. You can see that as follows: Head[_?(FreeQ[#, Complex] &)] PatternTest Head[_?(FreeQ[#, Complex]) &] Function Quite different. What you need is the former, not the latter. I don't think there is anything really misleading or ambiguous here as long as you remember that there is such a thing as precedence. Andrzej Kozlowski On 13 Apr 2012, at 10:57, Barrie Stokes wrote: > In support of my tireless campaign for the Esc-fn-Esc notation for pure functions, I submit > > Cases[{2^(Complex[ 2, 3 ]) + 3 , 2 , 3 x }, _?(s \[Function] FreeQ[s, Complex ]) ]. > > I have never been comfortable with the fact that > > (#^3 &)[x] > > and > > (#^3) &[x] > > give the same result. > > Admittedly, in the above case, your > > Cases[{2^(Complex [2, 3] ) + 3 , 2 , 3 x }, _?(FreeQ[ #, Complex ] &)] > > works whereas > > Cases [{2^(Complex [ 2, 3 ]) + 3 , 2 , 3 x }, _?(FreeQ [#, Complex ]) & ] > > doesn't, which I think adds weight to my point. Sometimes (&) and ()& are equivalent, sometimes not. Avoiding the question as to where the "&" goes, inside the "()" or outside, is in the spirit of no ambiguity in Mathematica code, IMHO. > > I just noted that the online help entry "tutorial/PureFunctions" mostly does not use parentheses, except for an example involving Fold, thus avoiding my dilemma. Hmm. It also says: > > "When you use the ampersand notation for pure functions, you must be careful about the grouping of pieces in your input. As shown in "Operator Input Forms" the ampersand notation has fairly low precedence, which means that you can type expressions like #1+#2& without parentheses. On the other hand, if you want, for example, to set an option to be a pure function, you need to use parentheses, as in option->(fun&). " > > I like to avoid @, #, and * because - especially in groups - they still remind me of swearing in a comic. > > Cheers > > Barrie > >>>> On 12/04/2012 at 4:36 pm, in message <201204120636.CAA05485 at smc.vnet.net>, Ray > Koopman <koopman at sfu.ca> wrote: >> On Apr 11, 3:26 pm, Brentt <brenttnew... at gmail.com> wrote: >>> Hello, I have a list of expressions, and I am trying to use DeleteCases to >>> delete expressions in the list which contain a certain head *anywhere* in >>> the expression. Setting the levelspec for DeleteCases to Infinity almost >>> works, except I want to delete the entire expression containing the head >>> from the list as opposed to just the head itself while leaving the est of >>> the expression in the list. >>> >>> e.g. >>> I want this >>> DeleteCases[{2^(Complex[2,3])+3 , 2 ,3x }, Complex[_,_], Infinity ] >>> >>> to output this >>> {2,3x} >>> Getting rid of any expression that has a complex number in it. >>> >>> How does one go about this? >> >> Cases[{2^(Complex[2,3])+3 , 2 , 3x }, _?(FreeQ[#,Complex]&)] >> >> {2, 3 x} > >

**References**:**Re: Deleting expressions containing a certain head, and a plea for Esc-fn-Esc***From:*Barrie Stokes <Barrie.Stokes@newcastle.edu.au>