Counting occurrences question

Hannah Schroeter uk1o@rz.uni-karlsruhe.de
Wed, 5 Jun 2002 20:11:41 +0200


Hello!

On Wed, Jun 05, 2002 at 07:04:28PM +0100, Andy Fugard wrote:
> >===== Original Message From "xoo" <ixoo21@hotmail.com> =====
> >hi.. i was just wondering if some body could give a simple equation for the 
> following situation.other than recursion plz..

> >occurrences :: Eq a => a -> [a] -> [a]
> >--occurrences xs ys returns the number of times that xs  occurs in ys


> You may find it easier if you make

>   occurrences :: Eq a => a -> [a] -> Integer

> since it would seem it is to return a number, and not another list!

Yep. And not call the parameter for the single 'a' "xs". That's
misleading.

> Also I would guess the function will have a form something like

>   occurrences x xs = foldr (countOp x) 0 xs
>       where countOp :: Eq a => a -> a -> Integer -> Integer
>             ...

> [...]

Why not combine filter and length appropriately?

Kind regards,

Hannah.