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-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@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:  Function to compute the mean (David James)


----------------------------------------------------------------------

Message: 1
Date: Mon, 10 May 2021 11:43:03 +0000
From: David James <dj112...@outlook.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Function to compute the mean
Message-ID:
        
<am6pr04mb42141e9503a0db6e717c02aeb6...@am6pr04mb4214.eurprd04.prod.outlook.com>
        
Content-Type: text/plain; charset="windows-1252"

Hello – I wanted to add some comments. mean is as you describe.

mean1 as defined can take a list of any Real type, and return any Fractional 
type. These types need not be the same, and it’s up to the *caller* of mean1 to 
say what types they want to give and get returned, e.g. the following is 
possible:

> :m +Data.Ratio
> :m +Data.Complex
> mean1 [6%5, 2%3] :: Complex Double
0.9333333333333333 :+ 0.0

This may not be what you were expecting, and maybe you want something like this:

mean2 :: (Fractional a) => [a] -> a
mean2 xs = sum xs / genericLength xs

(and note the realToFrac is then unces




From: Beginners <beginners-boun...@haskell.org> on behalf of Joe King 
<joeking1...@yahoo.com>
Sent: Saturday, May 8, 2021 10:39:50 AM
To: beginners@haskell.org <beginners@haskell.org>
Subject: [Haskell-beginners] Function to compute the mean

Greeetings I am new here and pretty new to Haskell.

I was wondering what are the relative advanatges/disadvatnages of specifying a 
mean function in these two ways:

mean :: [Double] -> Double
mean xs = sum xs / fromIntegral (length xs)

and

mean1 :: (Real a, Fractional b) => [a] -> b
mean1 xs = realToFrac (sum xs) / genericLength xs

I understand that mean1 has the advantage that it can be called with lists of 
any Real type, so would work with things like

foo :: [Int]
foo = [1,2,3]

mean foo
-- type mismatch error

mean1 foo
-- no error

But suppose that I know I will only ever use lists of Double, is there still 
any advantage (or disadvantage of using mean1). For example is there any 
performance benefit by using mean in that case since mean1 has additional 
function evaluation.

Are there any other considerations ?

Thanks in advance
JK
_______________________________________________
Beginners mailing list
Beginners@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/20210510/1a3fe1ff/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: C41BD4BD0CBD4F519B49D2A93C03CE4A.png
Type: image/png
Size: 144 bytes
Desc: C41BD4BD0CBD4F519B49D2A93C03CE4A.png
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20210510/1a3fe1ff/attachment-0001.png>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 154, Issue 4
*****************************************

Reply via email to