Re: Not[OddQ] is not the same as EvenQ (sometimes)
- To: mathgroup at smc.vnet.net
- Subject: [mg3898] Re: Not[OddQ] is not the same as EvenQ (sometimes)
- From: Robert Knapp <rknapp>
- Date: Sat, 4 May 1996 23:24:03 -0400
- Organization: Wolfram Research, Inc.
- Sender: owner-wri-mathgroup at wolfram.com
Arnold Seiken wrote:
>
> Dear Mathematica experts,
>
> Position[{2,3,4,5,6,7}, x_?(EvenQ[#]&)]
> {{1}, {3}, {5}}
> Position[{2,3,4,5,6,7}, x_?(!OddQ[#]&)]
> {{0}, {1}, {3}, {5}}
> Therefore, for this example, the pattern x_?(EvenQ[#]&) is not equivalent to
> the pattern x_?(!OddQ[#]&). The latter matches with the head List of {2,3,4,5,6,7}, the former does not. But
> Position[{-2,3,4,-5,6,7}, x_?(NonNegative[#]&)]
> {{2}, {3}, {5}, {6}}
> Position[{-2,3,4,-5,6,7}, x_?(!Negative[#]&)]
> {{2}, {3}, {5}, {6}}
> shows that the Head List is sometimes ignored by Position. Finally
> Position[{1,3,5}, x_?(!OddQ[#]&)]
> {{0}}
> Position[{2,3,4,5,6,7}, x_?(!NumberQ[#]&)]
> {{0}, {}}
> Position[{2,3,4,5,6,7}, x_?(!Positive[#]&)]
> {}
> seems to show that there are really three possible outcomes when using Not in a pattern involving the Position command. This does not occur for either the Cou
>
> Count[{1,3,5,7}, x_?(EvenQ[#]&)]
> 0
> Count[{1,3,5,7}, x_?(!OddQ[#]&)]
> 0
> Any explanations?
>
The explanation is that Position looks at all levels of it argument,
including the head. Look at the FullForm to the list in question:
In[1]:= expr = {2,3,4,5,6,7};
In[2]:= FullForm[expr]
Out[2]//FullForm= List[2, 3, 4, 5, 6, 7]
it head is the symbol List. Furthermore, by convention, the "0th"
element of an expression is its head. Thus:
In[3]:= expr[[0]]
Out[3]= List
now the symobol List is neither Odd nor Even, so it does not satisfy
EvenQ, but it does satisfy Not[OddQ[#]]& . This is why Position is
giving {{0},...} in that case.
On the other hand, Count does not by default go into the heads. This
can be changed by an option:
In[4]:= Count[{1,3,5,7},x_?(!OddQ[#]&),Heads->True]
Out[4]= 1
In[5]:= Count[{1, 3, 5, 7},x_?EvenQ,Heads->True]
Out[5]= 0
Rob Knapp
Wolfram Research, Inc.
http://www.wri.com/~rknapp
==== [MESSAGE SEPARATOR] ====