[Haskell-beginners] Beginners Digest, Vol 99, Issue 13
Tushar Tyagi
tushar4r at gmail.com
Fri Sep 23 03:31:18 UTC 2016
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" <laiboonh at gmail.com> wrote:
> 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, <beginners-request at haskell.org> wrote:
>
>> Send Beginners mailing list submissions to
>> beginners at 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 at haskell.org
>>
>> You can reach the person managing the list at
>> beginners-owner at 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 <laiboonh at gmail.com>
>> To: beginners at haskell.org
>> Subject: [Haskell-beginners] Newbie question about function type
>> constraints
>> Message-ID:
>> <CAJdQgg=jJYfdRsq+QfBr1aC-hS1ft9+4Sp4jopoktg=JnDMirg at mail.
>> gmail.com>
>> 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)
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL: <http://mail.haskell.org/pipermail/beginners/attachments/
>> 20160922/45389bc3/attachment-0001.html>
>>
>> ------------------------------
>>
>> Message: 2
>> Date: Thu, 22 Sep 2016 19:36:04 +0530
>> From: Tushar Tyagi <tushar4r at gmail.com>
>> To: The Haskell-Beginners Mailing List - Discussion of primarily
>> beginner-level topics related to Haskell <beginners at haskell.org>
>> Subject: Re: [Haskell-beginners] Newbie question about function type
>> constraints
>> Message-ID:
>> <CAEDPzin_vcZA+hh9ZLFo-k91Bn63vaemgQJKqN_P4hsAk+ALzQ at mail.
>> gmail.com>
>> Content-Type: text/plain; charset="utf-8"
>>
>> 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-0001.html>
>>
>> ------------------------------
>>
>> Message: 3
>> Date: Thu, 22 Sep 2016 16:06:52 +0200
>> From: Imants Cekusins <imantc at gmail.com>
>> To: The Haskell-Beginners Mailing List - Discussion of primarily
>> beginner-level topics related to Haskell <beginners at haskell.org>
>> Subject: Re: [Haskell-beginners] Newbie question about function type
>> constraints
>> Message-ID:
>> <CAP1qinZcONa4X3nozriFYnbkkguRfBmcFp2r3wMoZ6H3aLGGSQ at mail.gm
>> ail.com>
>> Content-Type: text/plain; charset="utf-8"
>>
>> Hello,
>>
>> this works too:
>>
>> meanList :: (Fractional a) => [a] -> a
>> meanList xs = (sumList xs) / (lengthList xs)
>>
>>
>> sumList :: (Fractional a) => [a] -> a
>> sumList [] = 0
>> sumList (x:xs) = x + (sumList xs)
>>
>>
>> lengthList :: (Fractional a) => [t] -> a
>> lengthList [] = 0
>> lengthList (_:xs) = 1 + (lengthList xs)
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL: <http://mail.haskell.org/pipermail/beginners/attachments/
>> 20160922/4c967a0d/attachment-0001.html>
>>
>> ------------------------------
>>
>> Message: 4
>> Date: Thu, 22 Sep 2016 16:08:56 +0200
>> From: Harald Bögeholz <bo at ct.de>
>> To: The Haskell-Beginners Mailing List - Discussion of primarily
>> beginner-level topics related to Haskell <beginners at haskell.org>
>> Subject: Re: [Haskell-beginners] Newbie question about function type
>> constraints
>> Message-ID: <a134a975-6b0f-b364-7db9-6b6a0d99759a at ct.de>
>> Content-Type: text/plain; charset=utf-8
>>
>> Am 22.09.16 um 15:19 schrieb Lai Boon Hui:
>> > 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
>>
>> 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 ...
>>
>>
>> > 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)
>>
>> Hope this helps
>>
>>
>> --
>> Harald Bögeholz <bo at ct.de> (PGP key available from servers)
>> Redaktion c't Tel.: +49 511 5352-300 Fax: +49 511 5352-417
>>
>> int f[9814],b,c=9814,g,i;long a=1e4,d,e,h;
>> main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a)
>> while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d%g;}
>> (Arndt/Haenel)
>>
>> Affe Apfel Vergaser
>>
>> /*Heise Medien GmbH & Co. KG * Karl-Wiechert-Allee 10 * 30625 Hannover
>> Registergericht: Amtsgericht Hannover HRA 26709
>> Persönlich haftende Gesellschafterin: Heise Medien Geschäftsführung GmbH
>> Registergericht: Amtsgericht Hannover, HRB 60405
>> Geschäftsführer: Ansgar Heise, Dr. Alfons Schräder*/
>>
>>
>> ------------------------------
>>
>> Message: 5
>> Date: Thu, 22 Sep 2016 16:10:17 +0200
>> From: Sylvain Henry <sylvain at haskus.fr>
>> To: beginners at haskell.org
>> Subject: Re: [Haskell-beginners] Newbie question about function type
>> constraints
>> Message-ID: <66b119b3-ff51-74d7-80c5-d19450164508 at haskus.fr>
>> Content-Type: text/plain; charset="utf-8"; Format="flowed"
>>
>> Hi,
>>
>> You can define it, but in practice there is no instance of "a" that
>> satisfies both constraints: Integral a and Fractional a
>> > meanList ([1,2,3] :: [Int])
>>
>> <interactive>:4:1: error:
>> • No instance for (Fractional Int) arising from a use of ‘meanList’
>>
>> > meanList ([1,2,3] :: [Float])
>>
>> <interactive>:5:1: error:
>> • No instance for (Integral Float) arising from a use of ‘meanList’
>>
>> What you probably want is:
>> meanList :: (Integral a, Fractional b) => [a] -> b
>> meanList xs = fromIntegral (sumList xs) / fromIntegral (lengthList xs)
>>
>> Where we convert from the integral type "a" to the fractional type "b"
>> before performing the division.
>>
>> > meanList ([1,2,3] :: [Int])
>> 2.0
>>
>> Cheers
>> Sylvain
>>
>>
>> On 22/09/2016 15:19, Lai Boon Hui 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/1be73068/attachment-0001.html>
>>
>> ------------------------------
>>
>> Message: 6
>> Date: Thu, 22 Sep 2016 16:14:48 +0200
>> From: Sylvain Henry <sylvain at haskus.fr>
>> To: beginners at haskell.org
>> Subject: Re: [Haskell-beginners] Newbie question about function type
>> constraints
>> Message-ID: <074cbf97-2316-9d70-3fb8-7c8c9904c602 at haskus.fr>
>> Content-Type: text/plain; charset=utf-8; format=flowed
>>
>> 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
>>
>>
>> ------------------------------
>>
>> Message: 7
>> Date: Thu, 22 Sep 2016 17:26:36 -0600
>> From: Dimitri DeFigueiredo <defigueiredo at ucdavis.edu>
>> To: Haskell Cafe <haskell-cafe at haskell.org>, The Haskell-Beginners
>> Mailing List - Discussion of primarily beginner-level topics
>> related
>> to Haskell <beginners at haskell.org>
>> Subject: [Haskell-beginners] The meaning of categories constructed
>> from HASK
>> Message-ID: <36383e22-0b32-426d-7c9f-5e611bbca233 at ucdavis.edu>
>> Content-Type: text/plain; charset=utf-8
>>
>> In category theory, there are many ways one can make new categories out
>> of an old one.
>>
>> In particular, given a category C one can construct:
>>
>> 1. The arrows category of C:
>> arrows in C become objects and
>> commutative squares in C become arrows
>> 2. The slice category of C given an object A:
>> arrows into a distinguished object A become objects in the slice
>> commutative triangles become arrows
>>
>> There are also functors going from C to these new categories (and back).
>>
>> Are these constructed categories useful when C = `Hask` (the category of
>> haskell types and functions)?
>> What do they represent in programming terms?
>>
>> In other words, is there intuition for what the arrows category of Hask
>> is?
>> What about the slice category of Hask over a specific type?
>> Do the functors between these match some programming abstractions?
>>
>> Any pointers are much appreciated.
>>
>> Thanks,
>>
>> Dimitri
>>
>>
>>
>>
>> ------------------------------
>>
>> Subject: Digest Footer
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>> ------------------------------
>>
>> End of Beginners Digest, Vol 99, Issue 13
>> *****************************************
>>
>
>
>
> --
> Best Regards,
> Boon Hui
>
> _______________________________________________
> 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/20160923/7b6523b3/attachment-0001.html>
More information about the Beginners
mailing list