Having contraints like 'Fractional Int => IO ()'

Pixel pixel@mandrakesoft.com
13 Nov 2001 15:07:25 +0100


Jesper Louis Andersen <jlouis@diku.dk> writes:

> This problem has had my attention for a while now. I hope someone would
> like to help me out on the problem.
> 
> I have a simple average function defined as:
> 
> mean            :: (Fractional a) => [a] -> a
> mean l          = (sum l)/ fromIntegral (length l)

due to the absence of subtyping, things like this will work only if you don't
precise the type, keeping class constraints.

-- length2 :: Num a => [b] -> a
length2 []     = 0
length2 (x:xs) = 1 + length2 xs

-- mean :: Fractional a => [a] -> a
mean l = sum l / length2 l

m = (mean [1,0,0] :: Rational, mean [1,0,0] :: Double)

gives 1%3 and 0.333333


(thanks to pad for pinpointing the haskell solution, 
 merd solution is http://merd.sf.net/inference.txt)