<p dir="ltr">You can read more about Numbers here: <br>
<a href="https://www.haskell.org/tutorial/numbers.html">https://www.haskell.org/tutorial/numbers.html</a></p>
<p dir="ltr">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. :) </p>
<p dir="ltr">Typed using my phone, so excuse my brevity. </p>
<div class="gmail_extra"><br><div class="gmail_quote">On 23 Sep 2016 6:14 a.m., "Lai Boon Hui" <<a href="mailto:laiboonh@gmail.com">laiboonh@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi All,<div><br></div><div>i am overwhelmed by all the helpful responses. Thanks guys.</div><div><br></div><div>I am more curious about why</div><div><br></div><div><div>meanList :: (Num a, Fractional b) => [a] -> b</div><div>meanList xs = (sumList xs) / (lengthList xs)</div></div><div><br></div><div>does not compile.</div><div><br></div><div>'a' being a Num type seems perfectly fine, (/) returns a Fractional type hence 'b' being Fractional seems also fine.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 23, 2016 at 7:13 AM, <span dir="ltr"><<a href="mailto:beginners-request@haskell.org" target="_blank">beginners-request@haskell.org</a><wbr>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Send Beginners mailing list submissions to<br>
<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
or, via email, send a message with subject or body 'help' to<br>
<a href="mailto:beginners-request@haskell.org" target="_blank">beginners-request@haskell.org</a><br>
<br>
You can reach the person managing the list at<br>
<a href="mailto:beginners-owner@haskell.org" target="_blank">beginners-owner@haskell.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than "Re: Contents of Beginners digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
1. Newbie question about function type constraints (Lai Boon Hui)<br>
2. Re: Newbie question about function type constraints<br>
(Tushar Tyagi)<br>
3. Re: Newbie question about function type constraints<br>
(Imants Cekusins)<br>
4. Re: Newbie question about function type constraints<br>
(Harald Bögeholz)<br>
5. Re: Newbie question about function type constraints<br>
(Sylvain Henry)<br>
6. Re: Newbie question about function type constraints<br>
(Sylvain Henry)<br>
7. The meaning of categories constructed from HASK<br>
(Dimitri DeFigueiredo)<br>
<br>
<br>
------------------------------<wbr>------------------------------<wbr>----------<br>
<br>
Message: 1<br>
Date: Thu, 22 Sep 2016 21:19:12 +0800<br>
From: Lai Boon Hui <<a href="mailto:laiboonh@gmail.com" target="_blank">laiboonh@gmail.com</a>><br>
To: <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><br>
Subject: [Haskell-beginners] Newbie question about function type<br>
constraints<br>
Message-ID:<br>
<CAJdQgg=jJYfdRsq+QfBr1aC-hS1f<wbr>t9+4Sp4jopoktg=<a href="mailto:JnDMirg@mail.gmail.com" target="_blank">JnDMirg@mail.<wbr>gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Hi, can someone explain to me why i cannot define meanList as:<br>
<br>
meanList :: (Integral a, Fractional b) => [a] -> a<br>
meanList xs = (sumList xs) / (lengthList xs)<br>
<br>
I want to restrict the function to only accept lists like [1,2,3] and<br>
return answer 2.0<br>
<br>
<br>
sumList :: (Num a) => [a] -> a<br>
sumList [] = 0<br>
sumList (x:xs) = x + (sumList xs)<br>
<br>
lengthList :: (Num a) => [t] -> a<br>
lengthList [] = 0<br>
lengthList (_:xs) = 1 + (lengthList xs)<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.haskell.org/pipermail/beginners/attachments/20160922/45389bc3/attachment-0001.html" rel="noreferrer" target="_blank">http://mail.haskell.org/piper<wbr>mail/beginners/attachments/<wbr>20160922/45389bc3/attachment-<wbr>0001.html</a>><br>
<br>
------------------------------<br>
<br>
Message: 2<br>
Date: Thu, 22 Sep 2016 19:36:04 +0530<br>
From: Tushar Tyagi <<a href="mailto:tushar4r@gmail.com" target="_blank">tushar4r@gmail.com</a>><br>
To: The Haskell-Beginners Mailing List - Discussion of primarily<br>
beginner-level topics related to Haskell <<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>><br>
Subject: Re: [Haskell-beginners] Newbie question about function type<br>
constraints<br>
Message-ID:<br>
<<a href="mailto:CAEDPzin_vcZA%2Bhh9ZLFo-k91Bn63vaemgQJKqN_P4hsAk%2BALzQ@mail.gmail.com" target="_blank">CAEDPzin_vcZA+hh9ZLFo-k91Bn63<wbr>vaemgQJKqN_P4hsAk+ALzQ@mail.<wbr>gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
What happens if you change the signature of meanList to<br>
<br>
meanList :: ( Fractional b) => [b] ->b<br>
<br>
The integrals in [1,2,3] would be converted to [1.0, 2.0, 3.0] before you<br>
act upon them.<br>
<br>
On 22 Sep 2016 6:49 p.m., "Lai Boon Hui" <<a href="mailto:laiboonh@gmail.com" target="_blank">laiboonh@gmail.com</a>> wrote:<br>
<br>
Hi, can someone explain to me why i cannot define meanList as:<br>
<br>
meanList :: (Integral a, Fractional b) => [a] -> a<br>
meanList xs = (sumList xs) / (lengthList xs)<br>
<br>
I want to restrict the function to only accept lists like [1,2,3] and<br>
return answer 2.0<br>
<br>
<br>
sumList :: (Num a) => [a] -> a<br>
sumList [] = 0<br>
sumList (x:xs) = x + (sumList xs)<br>
<br>
lengthList :: (Num a) => [t] -> a<br>
lengthList [] = 0<br>
lengthList (_:xs) = 1 + (lengthList xs)<br>
<br>
<br>
<br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.haskell.org/pipermail/beginners/attachments/20160922/a092897b/attachment-0001.html" rel="noreferrer" target="_blank">http://mail.haskell.org/piper<wbr>mail/beginners/attachments/<wbr>20160922/a092897b/attachment-<wbr>0001.html</a>><br>
<br>
------------------------------<br>
<br>
Message: 3<br>
Date: Thu, 22 Sep 2016 16:06:52 +0200<br>
From: Imants Cekusins <<a href="mailto:imantc@gmail.com" target="_blank">imantc@gmail.com</a>><br>
To: The Haskell-Beginners Mailing List - Discussion of primarily<br>
beginner-level topics related to Haskell <<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>><br>
Subject: Re: [Haskell-beginners] Newbie question about function type<br>
constraints<br>
Message-ID:<br>
<<a href="mailto:CAP1qinZcONa4X3nozriFYnbkkguRfBmcFp2r3wMoZ6H3aLGGSQ@mail.gmail.com" target="_blank">CAP1qinZcONa4X3nozriFYnbkkguR<wbr>fBmcFp2r3wMoZ6H3aLGGSQ@mail.gm<wbr>ail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Hello,<br>
<br>
this works too:<br>
<br>
meanList :: (Fractional a) => [a] -> a<br>
meanList xs = (sumList xs) / (lengthList xs)<br>
<br>
<br>
sumList :: (Fractional a) => [a] -> a<br>
sumList [] = 0<br>
sumList (x:xs) = x + (sumList xs)<br>
<br>
<br>
lengthList :: (Fractional a) => [t] -> a<br>
lengthList [] = 0<br>
lengthList (_:xs) = 1 + (lengthList xs)<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.haskell.org/pipermail/beginners/attachments/20160922/4c967a0d/attachment-0001.html" rel="noreferrer" target="_blank">http://mail.haskell.org/piper<wbr>mail/beginners/attachments/<wbr>20160922/4c967a0d/attachment-<wbr>0001.html</a>><br>
<br>
------------------------------<br>
<br>
Message: 4<br>
Date: Thu, 22 Sep 2016 16:08:56 +0200<br>
From: Harald Bögeholz <<a href="mailto:bo@ct.de" target="_blank">bo@ct.de</a>><br>
To: The Haskell-Beginners Mailing List - Discussion of primarily<br>
beginner-level topics related to Haskell <<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>><br>
Subject: Re: [Haskell-beginners] Newbie question about function type<br>
constraints<br>
Message-ID: <<a href="mailto:a134a975-6b0f-b364-7db9-6b6a0d99759a@ct.de" target="_blank">a134a975-6b0f-b364-7db9-6b6a0<wbr>d99759a@ct.de</a>><br>
Content-Type: text/plain; charset=utf-8<br>
<br>
Am 22.09.16 um 15:19 schrieb Lai Boon Hui:<br>
> Hi, can someone explain to me why i cannot define meanList as:<br>
><br>
> meanList :: (Integral a, Fractional b) => [a] -> a<br>
> meanList xs = (sumList xs) / (lengthList xs)<br>
><br>
> I want to restrict the function to only accept lists like [1,2,3] and<br>
> return answer 2.0<br>
<br>
It will work like this:<br>
<br>
meanList :: (Integral a, Fractional b) => [a] -> b<br>
meanList xs = fromIntegral (sumList xs) / (lengthList xs)<br>
<br>
You probably meant -> b in the type signature, that was a typo.<br>
<br>
And you need to insert fromIntegral to convert to Fractional before you<br>
can divide. Now that I see it I am beginning to wonder why it works,<br>
though, because I was just about to insert another fromIntegral before<br>
lengthList ...<br>
<br>
<br>
> sumList :: (Num a) => [a] -> a<br>
> sumList [] = 0<br>
> sumList (x:xs) = x + (sumList xs)<br>
><br>
> lengthList :: (Num a) => [t] -> a<br>
> lengthList [] = 0<br>
> lengthList (_:xs) = 1 + (lengthList xs)<br>
<br>
Hope this helps<br>
<br>
<br>
--<br>
Harald Bögeholz <<a href="mailto:bo@ct.de" target="_blank">bo@ct.de</a>> (PGP key available from servers)<br>
Redaktion c't Tel.: <a href="tel:%2B49%20511%205352-300" value="+495115352300" target="_blank">+49 511 5352-300</a> Fax: <a href="tel:%2B49%20511%205352-417" value="+495115352417" target="_blank">+49 511 5352-417</a><br>
<br>
int f[9814],b,c=9814,g,i;long a=1e4,d,e,h;<br>
main(){for(;b=c,c-=14;i=print<wbr>f("%04d",e+d/a),e=d%a)<br>
while(g=--b*2)d=h*b+a*(i?f[b]<wbr>:a/5),h=d/--g,f[b]=d%g;}<br>
(Arndt/Haenel)<br>
<br>
Affe Apfel Vergaser<br>
<br>
/*Heise Medien GmbH & Co. KG * Karl-Wiechert-Allee 10 * 30625 Hannover<br>
Registergericht: Amtsgericht Hannover HRA 26709<br>
Persönlich haftende Gesellschafterin: Heise Medien Geschäftsführung GmbH<br>
Registergericht: Amtsgericht Hannover, HRB 60405<br>
Geschäftsführer: Ansgar Heise, Dr. Alfons Schräder*/<br>
<br>
<br>
------------------------------<br>
<br>
Message: 5<br>
Date: Thu, 22 Sep 2016 16:10:17 +0200<br>
From: Sylvain Henry <<a href="mailto:sylvain@haskus.fr" target="_blank">sylvain@haskus.fr</a>><br>
To: <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><br>
Subject: Re: [Haskell-beginners] Newbie question about function type<br>
constraints<br>
Message-ID: <<a href="mailto:66b119b3-ff51-74d7-80c5-d19450164508@haskus.fr" target="_blank">66b119b3-ff51-74d7-80c5-d1945<wbr>0164508@haskus.fr</a>><br>
Content-Type: text/plain; charset="utf-8"; Format="flowed"<br>
<br>
Hi,<br>
<br>
You can define it, but in practice there is no instance of "a" that<br>
satisfies both constraints: Integral a and Fractional a<br>
> meanList ([1,2,3] :: [Int])<br>
<br>
<interactive>:4:1: error:<br>
• No instance for (Fractional Int) arising from a use of ‘meanList’<br>
<br>
> meanList ([1,2,3] :: [Float])<br>
<br>
<interactive>:5:1: error:<br>
• No instance for (Integral Float) arising from a use of ‘meanList’<br>
<br>
What you probably want is:<br>
meanList :: (Integral a, Fractional b) => [a] -> b<br>
meanList xs = fromIntegral (sumList xs) / fromIntegral (lengthList xs)<br>
<br>
Where we convert from the integral type "a" to the fractional type "b"<br>
before performing the division.<br>
<br>
> meanList ([1,2,3] :: [Int])<br>
2.0<br>
<br>
Cheers<br>
Sylvain<br>
<br>
<br>
On 22/09/2016 15:19, Lai Boon Hui wrote:<br>
> Hi, can someone explain to me why i cannot define meanList as:<br>
><br>
> meanList :: (Integral a, Fractional b) => [a] -> a<br>
> meanList xs = (sumList xs) / (lengthList xs)<br>
><br>
> I want to restrict the function to only accept lists like [1,2,3] and<br>
> return answer 2.0<br>
><br>
><br>
> sumList :: (Num a) => [a] -> a<br>
> sumList [] = 0<br>
> sumList (x:xs) = x + (sumList xs)<br>
><br>
> lengthList :: (Num a) => [t] -> a<br>
> lengthList [] = 0<br>
> lengthList (_:xs) = 1 + (lengthList xs)<br>
><br>
><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.haskell.org/pipermail/beginners/attachments/20160922/1be73068/attachment-0001.html" rel="noreferrer" target="_blank">http://mail.haskell.org/piper<wbr>mail/beginners/attachments/<wbr>20160922/1be73068/attachment-<wbr>0001.html</a>><br>
<br>
------------------------------<br>
<br>
Message: 6<br>
Date: Thu, 22 Sep 2016 16:14:48 +0200<br>
From: Sylvain Henry <<a href="mailto:sylvain@haskus.fr" target="_blank">sylvain@haskus.fr</a>><br>
To: <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><br>
Subject: Re: [Haskell-beginners] Newbie question about function type<br>
constraints<br>
Message-ID: <<a href="mailto:074cbf97-2316-9d70-3fb8-7c8c9904c602@haskus.fr" target="_blank">074cbf97-2316-9d70-3fb8-7c8c9<wbr>904c602@haskus.fr</a>><br>
Content-Type: text/plain; charset=utf-8; format=flowed<br>
<br>
On 22/09/2016 16:08, Harald Bögeholz wrote:<br>
> It will work like this:<br>
> meanList :: (Integral a, Fractional b) => [a] -> b<br>
> meanList xs = fromIntegral (sumList xs) / (lengthList xs)<br>
><br>
> You probably meant -> b in the type signature, that was a typo.<br>
><br>
> And you need to insert fromIntegral to convert to Fractional before you<br>
> can divide. Now that I see it I am beginning to wonder why it works,<br>
> though, because I was just about to insert another fromIntegral before<br>
> lengthList ...<br>
It works because in this case lengthList uses the fractional type b to<br>
perfom its summation (it doesn't care about the type of the elements in xs).<br>
<br>
Cheers<br>
Sylvain<br>
<br>
<br>
------------------------------<br>
<br>
Message: 7<br>
Date: Thu, 22 Sep 2016 17:26:36 -0600<br>
From: Dimitri DeFigueiredo <<a href="mailto:defigueiredo@ucdavis.edu" target="_blank">defigueiredo@ucdavis.edu</a>><br>
To: Haskell Cafe <<a href="mailto:haskell-cafe@haskell.org" target="_blank">haskell-cafe@haskell.org</a>>, The Haskell-Beginners<br>
Mailing List - Discussion of primarily beginner-level topics related<br>
to Haskell <<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>><br>
Subject: [Haskell-beginners] The meaning of categories constructed<br>
from HASK<br>
Message-ID: <<a href="mailto:36383e22-0b32-426d-7c9f-5e611bbca233@ucdavis.edu" target="_blank">36383e22-0b32-426d-7c9f-5e611<wbr>bbca233@ucdavis.edu</a>><br>
Content-Type: text/plain; charset=utf-8<br>
<br>
In category theory, there are many ways one can make new categories out<br>
of an old one.<br>
<br>
In particular, given a category C one can construct:<br>
<br>
1. The arrows category of C:<br>
arrows in C become objects and<br>
commutative squares in C become arrows<br>
2. The slice category of C given an object A:<br>
arrows into a distinguished object A become objects in the slice<br>
commutative triangles become arrows<br>
<br>
There are also functors going from C to these new categories (and back).<br>
<br>
Are these constructed categories useful when C = `Hask` (the category of<br>
haskell types and functions)?<br>
What do they represent in programming terms?<br>
<br>
In other words, is there intuition for what the arrows category of Hask is?<br>
What about the slice category of Hask over a specific type?<br>
Do the functors between these match some programming abstractions?<br>
<br>
Any pointers are much appreciated.<br>
<br>
Thanks,<br>
<br>
Dimitri<br>
<br>
<br>
<br>
<br>
------------------------------<br>
<br>
Subject: Digest Footer<br>
<br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
<br>
<br>
------------------------------<br>
<br>
End of Beginners Digest, Vol 99, Issue 13<br>
******************************<wbr>***********<br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div data-smartmail="gmail_signature"><div dir="ltr">Best Regards,<div>Boon Hui</div></div></div>
</div>
<br>______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div></div>