The problem is that you need to convert (length xs) to a Num, then return a Fractional.
On Fri, Jul 1, 2011 at 2:07 PM, Nathan Howell <nathan.d.how...@gmail.com> wrote: > (/) operates on a Fractional instance... butĀ length returns an Int, which is > not a Fractional. > You can convert the Int to a Fractional instance: > mean xs = sum xs / fromIntegral (length xs) > or try an integer division: > mean xs = sum xs `div` length xs > -n > On Thu, Jun 30, 2011 at 10:55 PM, Ruohao Li <liruo...@gmail.com> wrote: >> >> Hi guys, >> I just started learning some Haskell. I want to implement a mean function >> to compute the mean of a list. The signature of the function is: >> mean :: (Num a, Fractional b) => [a] -> b >> But when I implement this simple function, the compiler keep whining at me >> on type errors. I know this is wrong: >> mean xs = sum xs / length xs >> But how to get it right? Thanks. >> _______________________________________________ >> Haskell-Cafe mailing list >> Haskell-Cafe@haskell.org >> http://www.haskell.org/mailman/listinfo/haskell-cafe >> > > > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > > _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe