There is a chapter in Real World Haskell [1] devoted to this exact question on this exact piece of code. <br><br>hth,<br>-deech<br><br>[1] <a href="http://book.realworldhaskell.org/read/profiling-and-optimization.html">http://book.realworldhaskell.org/read/profiling-and-optimization.html</a><br>
<br><div class="gmail_quote">On Thu, Apr 23, 2009 at 8:52 AM, Daniel Carrera <span dir="ltr">&lt;<a href="mailto:daniel.carrera@theingots.org">daniel.carrera@theingots.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">Daniel Fischer wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Try explicitly converting the length to the appropriate type:<br>
<br>
average xs = sum xs / fromIntegral (length xs)<br>
</blockquote>
<br></div>
Thanks. Could you help me understand what&#39;s happening?<br>
<br>
1. length returns Int.<br>
2. sum returns Num.<br>
3. (/) wants Fractional.<br>
<br>
It looks like (/) is happy with Num but doesn&#39;t like Int. This surprises me. I would have thought that Fractional is a kind of Num and Int is a kind of Fractional, so a function that expects Fractional would be happy with an Int but maybe not with a Num. But clearly that&#39;s not the way it works.<br>

<br>
&#39;fromIntegral&#39; converts Int to Num. So obviously, Num is good and Int is bad. But I don&#39;t really get why.<div class="im"><br>
<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
will yield a working (albeit inefficient)<br>
average :: Fractional a =&gt; [a] -&gt; a<br>
</blockquote>
<br></div>
Why is it inefficient? How would you make it efficient?<br>
<br>
Thanks for the help.<br>
<br>
Cheers,<div><div></div><div class="h5"><br>
Daniel.<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br>