Re: [Haskell-beginners] Beginners Digest, Vol 99, Issue 13

Hi All,
i am overwhelmed by all the helpful responses. Thanks guys.
I am more curious about why
meanList :: (Num a, Fractional b) => [a] -> b
meanList xs = (sumList xs) / (lengthList xs)
does not compile.
'a' being a Num type seems perfectly fine, (/) returns a Fractional type
hence 'b' being Fractional seems also fine.
On Fri, Sep 23, 2016 at 7:13 AM,
Send Beginners mailing list submissions to beginners@haskell.org
To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-request@haskell.org
You can reach the person managing the list at beginners-owner@haskell.org
When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..."
Today's Topics:
1. Newbie question about function type constraints (Lai Boon Hui) 2. Re: Newbie question about function type constraints (Tushar Tyagi) 3. Re: Newbie question about function type constraints (Imants Cekusins) 4. Re: Newbie question about function type constraints (Harald Bögeholz) 5. Re: Newbie question about function type constraints (Sylvain Henry) 6. Re: Newbie question about function type constraints (Sylvain Henry) 7. The meaning of categories constructed from HASK (Dimitri DeFigueiredo)
----------------------------------------------------------------------
Message: 1 Date: Thu, 22 Sep 2016 21:19:12 +0800 From: Lai Boon Hui
To: beginners@haskell.org Subject: [Haskell-beginners] Newbie question about function type constraints Message-ID: Content-Type: text/plain; charset="utf-8" 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)

You can read more about Numbers here:
https://www.haskell.org/tutorial/numbers.html
In your implementation sumList and lengthList both return 'Num' which
doesn't define a division operator. So you have to convert them into
fractional by either changing the signatures of these 2 functions from Num
to Fractional, or use fromIntegral function, (or something else) . Two of
these approaches have been suggested by people here. :)
Typed using my phone, so excuse my brevity.
On 23 Sep 2016 6:14 a.m., "Lai Boon Hui"
Hi All,
i am overwhelmed by all the helpful responses. Thanks guys.
I am more curious about why
meanList :: (Num a, Fractional b) => [a] -> b meanList xs = (sumList xs) / (lengthList xs)
does not compile.
'a' being a Num type seems perfectly fine, (/) returns a Fractional type hence 'b' being Fractional seems also fine.
On Fri, Sep 23, 2016 at 7:13 AM,
wrote: Send Beginners mailing list submissions to beginners@haskell.org
To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-request@haskell.org
You can reach the person managing the list at beginners-owner@haskell.org
When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..."
Today's Topics:
1. Newbie question about function type constraints (Lai Boon Hui) 2. Re: Newbie question about function type constraints (Tushar Tyagi) 3. Re: Newbie question about function type constraints (Imants Cekusins) 4. Re: Newbie question about function type constraints (Harald Bögeholz) 5. Re: Newbie question about function type constraints (Sylvain Henry) 6. Re: Newbie question about function type constraints (Sylvain Henry) 7. The meaning of categories constructed from HASK (Dimitri DeFigueiredo)
----------------------------------------------------------------------
Message: 1 Date: Thu, 22 Sep 2016 21:19:12 +0800 From: Lai Boon Hui
To: beginners@haskell.org Subject: [Haskell-beginners] Newbie question about function type constraints Message-ID: Content-Type: text/plain; charset="utf-8" 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)
participants (2)
-
Lai Boon Hui
-
Tushar Tyagi