[Haskell-cafe] General function to count list elements?
nowgate at yahoo.com
Sat Apr 18 11:30:04 EDT 2009
Though I haven't tried it out, it's trying to use my function to count functions.
The first argument is the identity function.
The second argument is a list of a different form of the identity function.
Though the two identity functions, given the same input, would produce the same output, I doubt they would be equal.
So my guess at an answer would be zero.
--- On Sat, 4/18/09, Eugene Kirpichov <ekirpichov at gmail.com> wrote:
From: Eugene Kirpichov <ekirpichov at gmail.com>
Subject: Re: [Haskell-cafe] General function to count list elements?
To: "michael rice" <nowgate at yahoo.com>
Cc: haskell-cafe at haskell.org
Date: Saturday, April 18, 2009, 11:03 AM
count (\x -> x) (replicate 10 (\y -> if 1==1 then y else undefined))
2009/4/18 michael rice <nowgate at yahoo.com>:
> 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 at 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 )
> 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.
> Not sure what it's trying to tell me other than I need an (Eq a) somewhere.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
Web IR developer, market.yandex.ru
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe