MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Not[OddQ] is not the same as EvenQ (sometimes)

  • To: mathgroup at
  • Subject: [mg3954] Re: [mg3885] Not[OddQ] is not the same as EvenQ (sometimes)
  • From: Allan Hayes <hay at>
  • Date: Sat, 11 May 1996 23:52:46 -0400
  • Sender: owner-wri-mathgroup at

"Arnold Seiken" <SEIKENA at>
in [mg3885] Not[OddQ] is not the same as EvenQ (sometimes)

>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.
>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 Count or Cases command where you get what  
you >expect to get.
>Count[{1,3,5,7}, x_?(EvenQ[#]&)]
>Count[{1,3,5,7}, x_?(!OddQ[#]&)]
>Any explanations?

The differences that you notice are due, not to any arbitrary  
behaviour, but are because:

(1)functions ending in Q, like EvenQ, OddQ, NumberQ... always  
return True  or False, but  NonNegative, Negative, Positive can  
return NoneNegative[x]...  if Mma can't decide.
(2) in default behaviour, Position looks at and inside heads and at  
all levels; but Cases and Count do not look at or inside heads and  
look only at level 1 :

Examples re (2):
Position[{7,{9}}[3,{5}], x_?OddQ]
	{{0, 1}, {0, 2, 1}, {1}, {2, 1}}
Cases[{7,{9}}[3,{5}], x_?OddQ]
Count[{7,{9}}[3,{5}], x_?OddQ]

You can control the behaviour.
For example
Position[{7,{9}}[3,{5}], x_?OddQ,{2}]
	{{0, 1}, {2, 1}}
Position[{7,{9}}[3,{5}], x_?OddQ,{2}, Heads -> False]
	{{2, 1}}

Cases[{7,{9}}[3,{5}], x_?OddQ, {2}]
Cases[{7,{9}}[3,{5}], x_?OddQ, Heads ->True]

Allan Hayes
hay at


  • Prev by Date: Re: Q: OPtimal swap file settings with win3.11
  • Next by Date: Re: Re: Not[OddQ] is not the same as EvenQ (sometimes)
  • Previous by thread: Re: Not[OddQ] is not the same as EvenQ (sometimes)
  • Next by thread: Re: Re: Not[OddQ] is not the same as EvenQ (sometimes)