[Haskell-beginners] Newbie question about function type constraints

Tushar Tyagi tushar4r at gmail.com
Thu Sep 22 14:06:04 UTC 2016


What happens if you change the signature of meanList to

meanList :: ( Fractional b) => [b] ->b

The integrals in [1,2,3] would be converted to [1.0, 2.0, 3.0] before you
act upon them.

On 22 Sep 2016 6:49 p.m., "Lai Boon Hui" <laiboonh at gmail.com> wrote:

Hi, can someone explain to me why i cannot define meanList as:

meanList :: (Integral a, Fractional b) => [a] -> a
meanList xs = (sumList xs) / (lengthList xs)

I want to restrict the function to only accept lists like [1,2,3] and
return answer 2.0


sumList :: (Num a) => [a] -> a
sumList [] = 0
sumList (x:xs) = x + (sumList xs)

lengthList :: (Num a) => [t] -> a
lengthList [] = 0
lengthList (_:xs) = 1 + (lengthList xs)



_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160922/a092897b/attachment.html>


More information about the Beginners mailing list