2 ms·
Also the + operator is typed with Num; from a Haskell repl: λ> :t (+) (+) :: Num a => a -> a -> a In case of average we need /: λ> :t (/) (/) :: Frac
by ff_ 8y ago
Also the + operator is typed with Num; from a Haskell repl:
λ> :t (+)
(+) :: Num a => a -> a -> a
In case of average we need /:
λ> :t (/)
(/) :: Fractional a => a -> a -> a
So our type is going to be constrained by Fractional instead of Num). So an implementation could be (from here [1]):
import Data.List
average :: (Real a, Fractional b) => [a] -> b
average xs = realToFrac (sum xs) / genericLength xs
[1]: https://stackoverflow.com/questions/2376981 https://stackoverflow.com/questions/2376981