General function to count list elements?

Is there a general function to count list elements. I'm trying this count :: a -> [a] -> Int count x ys = length (filter (== x) ys) with this error upon loading ============= [michael@localhost ~]$ ghci count GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Main ( count.hs, interpreted ) count.hs:2:29: Could not deduce (Eq a) from the context () arising from a use of `==' at count.hs:2:29-32 Possible fix: add (Eq a) to the context of the type signature for `count' In the first argument of `filter', namely `(== x)' In the first argument of `length', namely `(filter (== x) ys)' In the expression: length (filter (== x) ys) Failed, modules loaded: none. Prelude> ============= Not sure what it's trying to tell me other than I need an (Eq a) somewhere. Michael

What should
count (\x -> x) (replicate 10 (\y -> if 1==1 then y else undefined))
return?
2009/4/18 michael rice
Is there a general function to count list elements. I'm trying this
count :: a -> [a] -> Int count x ys = length (filter (== x) ys)
with this error upon loading
=============
[michael@localhost ~]$ ghci count GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Main ( count.hs, interpreted )
count.hs:2:29: Could not deduce (Eq a) from the context () arising from a use of `==' at count.hs:2:29-32 Possible fix: add (Eq a) to the context of the type signature for `count' In the first argument of `filter', namely `(== x)' In the first argument of `length', namely `(filter (== x) ys)' In the expression: length (filter (== x) ys) Failed, modules loaded: none. Prelude>
=============
Not sure what it's trying to tell me other than I need an (Eq a) somewhere.
Michael
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Web IR developer, market.yandex.ru

Hello michael, Saturday, April 18, 2009, 6:56:20 PM, you wrote:
Is there a general function to count list elements. I'm trying this
you should add Eq restriction to type declaration since "==" operation belomngs to Eq class and your function may work only with types supporting comparision: count :: (Eq a) => a -> [a] -> Int -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

The problem is that you must note in the type signature the fact that 'a' must be a member of typeclass Eq in order to be able to use == count :: (Eq a) => a -> [a] -> Int count x ys = length (filter (== x) ys) JP michael rice wrote:
Is there a general function to count list elements. I'm trying this
count :: a -> [a] -> Int count x ys = length (filter (== x) ys)
with this error upon loading
=============
[michael@localhost ~]$ ghci count GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Main ( count.hs, interpreted )
count.hs:2:29: Could not deduce (Eq a) from the context () arising from a use of `==' at count.hs:2:29-32 Possible fix: add (Eq a) to the context of the type signature for `count' In the first argument of `filter', namely `(== x)' In the first argument of `length', namely `(filter (== x) ys)' In the expression: length (filter (== x) ys) Failed, modules loaded: none. Prelude>
=============
Not sure what it's trying to tell me other than I need an (Eq a) somewhere.
Michael
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Bulat Ziganshin
-
Eugene Kirpichov
-
Juan Pedro Bolivar Puente
-
michael rice