[Haskell-beginners] Newbie question about function type constraints
Sylvain Henry
sylvain at haskus.fr
Thu Sep 22 14:14:48 UTC 2016
On 22/09/2016 16:08, Harald Bögeholz wrote:
> It will work like this:
> meanList :: (Integral a, Fractional b) => [a] -> b
> meanList xs = fromIntegral (sumList xs) / (lengthList xs)
>
> You probably meant -> b in the type signature, that was a typo.
>
> And you need to insert fromIntegral to convert to Fractional before you
> can divide. Now that I see it I am beginning to wonder why it works,
> though, because I was just about to insert another fromIntegral before
> lengthList ...
It works because in this case lengthList uses the fractional type b to
perfom its summation (it doesn't care about the type of the elements in xs).
Cheers
Sylvain
More information about the Beginners
mailing list