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

Hi,
still does not compile
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)
meanList :: (Num a, Fractional b) => [a] -> b
meanList xs = (sumList xs) / (lengthList xs)
i know
meanList :: (Fractional b) => [b] -> b
meanList xs = (sumList xs) / (lengthList xs)
compiles. But why do i have to restrict the inputs to be a list of
Fractionals and not Nums??
On Fri, Sep 23, 2016 at 11:18 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. Re: Beginners Digest, Vol 99, Issue 13 (Tushar Tyagi)
----------------------------------------------------------------------
Message: 1 Date: Fri, 23 Sep 2016 09:01:18 +0530 From: Tushar Tyagi
To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Beginners Digest, Vol 99, Issue 13 Message-ID: Content-Type: text/plain; charset="utf-8" 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"
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,
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)

Because if you say that your function accept all types which are instances of Num, then it must accept *all* of them. Even those who do not define a division, or whose division has a different behaviour than the fractional one. Le 23/09/2016 à 19:53, Lai Boon Hui a écrit :
Hi,
still does not compile
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)
meanList :: (Num a, Fractional b) => [a] -> b meanList xs = (sumList xs) / (lengthList xs)
i know
meanList :: (Fractional b) => [b] -> b meanList xs = (sumList xs) / (lengthList xs)
compiles. But why do i have to restrict the inputs to be a list of Fractionals and not Nums??
On Fri, Sep 23, 2016 at 11:18 AM,
mailto:beginners-request@haskell.org> wrote: Send Beginners mailing list submissions to beginners@haskell.org mailto:beginners@haskell.org
To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners 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 mailto:beginners-request@haskell.org
You can reach the person managing the list at beginners-owner@haskell.org mailto: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. Re: Beginners Digest, Vol 99, Issue 13 (Tushar Tyagi)
----------------------------------------------------------------------
Message: 1 Date: Fri, 23 Sep 2016 09:01:18 +0530 From: Tushar Tyagi
mailto:tushar4r@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell mailto:beginners@haskell.org> Subject: Re: [Haskell-beginners] Beginners Digest, Vol 99, Issue 13 Message-ID:
mailto:CAEDPzikCmLEpiop-5BGbDaKZK1TifAu6ymF%2BjmJq_rEuMNN_Kg@mail.gmail.com> Content-Type: text/plain; charset="utf-8" You can read more about Numbers here: https://www.haskell.org/tutorial/numbers.html 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"
mailto:laiboonh@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,
mailto:beginners-request@haskell.org> wrote: > >> Send Beginners mailing list submissions to >> beginners@haskell.org mailto:beginners@haskell.org >> >> To subscribe or unsubscribe via the World Wide Web, visit >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners 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 mailto:beginners-request@haskell.org >> >> You can reach the person managing the list at >> beginners-owner@haskell.org mailto: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 mailto:laiboonh@gmail.com> >> To: beginners@haskell.org mailto:beginners@haskell.org >> Subject: [Haskell-beginners] Newbie question about function type >> constraints >> Message-ID: >> > gmail.com http://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) >>
participants (2)
-
ARJANEN Loïc Jean David
-
Lai Boon Hui