[Haskell-beginners] Average of numeric list

Nadav Chernin nadavchernin at gmail.com
Mon Apr 4 17:15:20 CEST 2011


Hello,

I tried to write function mean - average of numeric list.

mean::(Fractional a)=>[a]->a
mean a = (realToFrac (sum a)) / (realToFrac (length a))

But error occures:

              Could not deduce (Real a) from the context (Fractional b)
              arising from a use of `realToFrac'
To correct this function, i rewrite this function:

mean::(Real a, Fractional a)=>[a]->b
mean a = (realToFrac (sum a)) / (realToFrac (length a))

Is there most simple way to write this function?

Thanks, Nadav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110404/71c2c51a/attachment.htm>


More information about the Beginners mailing list