[Haskell-cafe] How to implement the mean function

Jack Henahan jhenahan at uvm.edu
Fri Jul 1 08:11:33 CEST 2011


Additionally, this SO question[0] is nearly identical, and provides a little more elaboration.

[0]:http://stackoverflow.com/questions/2376981/haskell-types-frustrating-a-simple-average-function

On Jul 1, 2011, at 2:07 AM, Ruohao Li wrote:

> For mean xs = sum xs / length xs, I got the following:
> 
> test.hs:8:10:
>     No instance for (Fractional Int)
>       arising from a use of `/' at test.hs:8:10-27
>     Possible fix: add an instance declaration for (Fractional Int)
>     In the expression: sum xs / length xs
>     In the definition of `mean': mean xs = sum xs / length xs
> 
> test.hs:8:10:
>     Couldn't match expected type `b' against inferred type `Int'
>       `b' is a rigid type variable bound by
>           the type signature for `mean' at test.hs:7:27
>     In the expression: sum xs / length xs
>     In the definition of `mean': mean xs = sum xs / length xs
> 
> test.hs:8:19:
>     Couldn't match expected type `a' against inferred type `Int'
>       `a' is a rigid type variable bound by
>           the type signature for `mean' at test.hs:7:13
>     In the second argument of `(/)', namely `length xs'
>     In the expression: sum xs / length xs
>     In the definition of `mean': mean xs = sum xs / length xs
> On Fri, Jul 1, 2011 at 2:00 PM, aditya siram <aditya.siram at gmail.com> wrote:
> What compiler errors are you getting?
> -deech
> 
> On Fri, Jul 1, 2011 at 12:55 AM, Ruohao Li <liruohao at gmail.com> wrote:
> > Hi guys,
> > 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:
> > mean :: (Num a, Fractional b) => [a] -> b
> > But when I implement this simple function, the compiler keep whining at me
> > on type errors. I know this is wrong:
> > mean xs = sum xs / length xs
> > But how to get it right? Thanks.
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-Cafe at haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
> >
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



====
"Computer Science is no more about computers than astronomy is about telescopes."
-- Edsger Dijkstra
====

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 398E692F.asc
Type: application/x-apple-msg-attachment
Size: 24295 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110701/a97bbc45/attachment.bin>
-------------- next part --------------


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 881 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110701/a97bbc45/attachment.pgp>


More information about the Haskell-Cafe mailing list