<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-GB" link="blue" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Hello – I wanted to add some comments. mean is as you describe.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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 *<b>caller</b>* of mean1 to say what types they want to give and get returned, e.g. the following is
 possible:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">> :m +Data.Ratio</p>
<p class="MsoNormal">> :m +Data.Complex</p>
<p class="MsoNormal">> mean1 [6%5, 2%3] :: Complex Double</p>
<p class="MsoNormal">0.9333333333333333 :+ 0.0</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">This may not be what you were expecting, and maybe you want something like this:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">mean2 :: (Fractional a) => [a] -> a</p>
<p class="MsoNormal">mean2 xs = sum xs / genericLength xs</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">(and note the realToFrac is then unces</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><img width="694" height="2" style="width:7.2291in;height:.0208in" id="Horizontal_x0020_Line_x0020_1" src="cid:image001.png@01D7459A.0824F980"><o:p></o:p></p>
<div id="divRplyFwdMsg">
<p class="MsoNormal"><b><span style="color:black">From:</span></b><span style="color:black"> Beginners <beginners-bounces@haskell.org> on behalf of Joe King <joeking1809@yahoo.com><br>
<b>Sent:</b> Saturday, May 8, 2021 10:39:50 AM<br>
<b>To:</b> beginners@haskell.org <beginners@haskell.org><br>
<b>Subject:</b> [Haskell-beginners] Function to compute the mean</span> <o:p></o:p></p>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<p class="MsoNormal">Greeetings I am new here and pretty new to Haskell.<br>
<br>
I was wondering what are the relative advanatges/disadvatnages of specifying a mean function in these two ways:<br>
<br>
mean :: [Double] -> Double<br>
mean xs = sum xs / fromIntegral (length xs)<br>
<br>
and<br>
<br>
mean1 :: (Real a, Fractional b) => [a] -> b<br>
mean1 xs = realToFrac (sum xs) / genericLength xs<br>
<br>
I understand that mean1 has the advantage that it can be called with lists of any Real type, so would work with things like <br>
<br>
foo :: [Int]<br>
foo = [1,2,3]<br>
<br>
mean foo<br>
-- type mismatch error<br>
<br>
mean1 foo<br>
-- no error<br>
<br>
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. <br>
<br>
Are there any other considerations ?<br>
<br>
Thanks in advance<br>
JK<br>
_______________________________________________<br>
Beginners mailing list<br>
Beginners@haskell.org<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><o:p></o:p></p>
</div>
</body>
</html>