For <font class="Apple-style-span" face="'courier new', monospace">mean xs = sum xs / fromIntegral (length xs)</font>, I got the following:<div><div><br></div><div>test.hs:8:10:</div><div> Could not deduce (Fractional a)</div>
<div> from the context (Num a, Fractional b)</div><div> arising from a use of `/' at test.hs:8:10-42</div><div> Possible fix:</div><div> add (Fractional a) to the context of the type signature for `mean'</div>
<div> In the expression: sum xs / fromIntegral (length xs)</div><div> In the definition of `mean':</div><div> mean xs = sum xs / fromIntegral (length xs)</div><div><br></div><div>test.hs:8:10:</div><div> Couldn't match expected type `b' against inferred type `a'</div>
<div> `b' is a rigid type variable bound by</div><div> the type signature for `mean' at test.hs:7:27</div><div> `a' is a rigid type variable bound by</div><div> the type signature for `mean' at test.hs:7:13</div>
<div> In the expression: sum xs / fromIntegral (length xs)</div><div> In the definition of `mean':</div><div> mean xs = sum xs / fromIntegral (length xs)</div><div><br></div><div>And the div way will do integer division, which is not what I want.</div>
<br><div class="gmail_quote">On Fri, Jul 1, 2011 at 2:07 PM, Nathan Howell <span dir="ltr"><<a href="mailto:nathan.d.howell@gmail.com">nathan.d.howell@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
(/) operates on a Fractional instance... but length returns an Int, which is not a Fractional.<div><br></div><div>You can convert the Int to a Fractional instance:</div><div>mean xs = sum xs / fromIntegral (length xs)</div>
<div><br></div><div>or try an integer division:</div><div><div>mean xs = sum xs `div` length xs</div><div><br></div><font color="#888888"><div>-n</div><br></font><div class="gmail_quote"><div><div></div><div class="h5">On Thu, Jun 30, 2011 at 10:55 PM, Ruohao Li <span dir="ltr"><<a href="mailto:liruohao@gmail.com" target="_blank">liruohao@gmail.com</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div class="h5">Hi guys,<div><br></div><div>I just started learning some Haskell. I want to implement a mean function to compute the mean of a list. The signature of the function is:</div>
<div><font face="'courier new', monospace">mean :: (Num a, Fractional b) => [a] -> b</font></div>
<div>But when I implement this simple function, the compiler keep whining at me on type errors. I know this is wrong:</div><div><font face="'courier new', monospace">mean xs = sum xs / length xs</font></div>
<div>But how to get it right? Thanks.</div>
<br></div></div><div class="im">_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></div></blockquote></div><br></div>
</blockquote></div><br></div>