
Arbitrary numerical types don't support division. (They're rings, not
fields.) The
(Fractional a) => [a] -> a
type is the best you can do.
- Cale
On 01/04/06, Matthias Fischmann
hi all,
this should be a quick one (for now I would be happy with a "that's impossible", just want to make sure I don't miss anything). I want to compute the average from a list of arbitrary numerical element type. I wanted to do this:
avg :: (Num a) => [a] -> a avg xs = sum (map fromNum xs) / (fromIntegral (length xs))
but it doesn't compile. All I could get to work is this:
avg :: (Fractional a) => [a] -> a avg xs = sum xs / (fromIntegral (length xs))
avgI :: (Integral a) => [a] -> Float avgI = avg . map fromIntegral
The two function names for the same thing are tolerable, but not very elegant. Is there another option?
Thanks, Matthias
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQFELsI5TXPx/Y0ym6oRAuQuAKDJzqus2beMm5WKfJBupPTesm6XcQCgjohh yKhGfl6Wv3t97ZGfBYTXKQM= =mmOH -----END PGP SIGNATURE-----
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe