How to implement the mean function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

How to implement the mean function

Ruohao Li
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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to implement the mean function

aditya siram-2
What compiler errors are you getting?
-deech

On Fri, Jul 1, 2011 at 12:55 AM, Ruohao Li <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to implement the mean function

Ruohao Li
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 <[hidden email]> wrote:
What compiler errors are you getting?
-deech

On Fri, Jul 1, 2011 at 12:55 AM, Ruohao Li <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to implement the mean function

Nathan Howell-2
In reply to this post by Ruohao Li
(/) operates on a Fractional instance... but length returns an Int, which is not a Fractional.

You can convert the Int to a Fractional instance:
mean xs = sum xs / fromIntegral (length xs)

or try an integer division:
mean xs = sum xs `div` length xs

-n

On Thu, Jun 30, 2011 at 10:55 PM, Ruohao Li <[hidden email]> 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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to implement the mean function

Lyndon Maydwell
The problem is that you need to convert (length xs) to a Num, then
return a Fractional.

On Fri, Jul 1, 2011 at 2:07 PM, Nathan Howell <[hidden email]> wrote:

> (/) operates on a Fractional instance... but length returns an Int, which is
> not a Fractional.
> You can convert the Int to a Fractional instance:
> mean xs = sum xs / fromIntegral (length xs)
> or try an integer division:
> mean xs = sum xs `div` length xs
> -n
> On Thu, Jun 30, 2011 at 10:55 PM, Ruohao Li <[hidden email]> 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
>> [hidden email]
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to implement the mean function

Ruohao Li
In reply to this post by Nathan Howell-2
For mean xs = sum xs / fromIntegral (length xs), I got the following:

test.hs:8:10:
    Could not deduce (Fractional a)
      from the context (Num a, Fractional b)
      arising from a use of `/' at test.hs:8:10-42
    Possible fix:
      add (Fractional a) to the context of the type signature for `mean'
    In the expression: sum xs / fromIntegral (length xs)
    In the definition of `mean':
        mean xs = sum xs / fromIntegral (length xs)

test.hs:8:10:
    Couldn't match expected type `b' against inferred type `a'
      `b' is a rigid type variable bound by
          the type signature for `mean' at test.hs:7:27
      `a' is a rigid type variable bound by
          the type signature for `mean' at test.hs:7:13
    In the expression: sum xs / fromIntegral (length xs)
    In the definition of `mean':
        mean xs = sum xs / fromIntegral (length xs)

And the div way will do integer division, which is not what I want.

On Fri, Jul 1, 2011 at 2:07 PM, Nathan Howell <[hidden email]> wrote:
(/) operates on a Fractional instance... but length returns an Int, which is not a Fractional.

You can convert the Int to a Fractional instance:
mean xs = sum xs / fromIntegral (length xs)

or try an integer division:
mean xs = sum xs `div` length xs

-n

On Thu, Jun 30, 2011 at 10:55 PM, Ruohao Li <[hidden email]> 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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe




_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to implement the mean function

Jack Henahan
In reply to this post by Ruohao Li
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 <[hidden email]> wrote:
> What compiler errors are you getting?
> -deech
>
> On Fri, Jul 1, 2011 at 12:55 AM, Ruohao Li <[hidden email]> 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
> > [hidden email]
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
> >
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe


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





_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

398E692F.asc (24K) Download Attachment
PGP.sig (899 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to implement the mean function

Ruohao Li
Thanks for the SO link, change the Num a constraint to Real a and using realToFrac then it just works.

On Fri, Jul 1, 2011 at 2:11 PM, Jack Henahan <[hidden email]> wrote:
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 <[hidden email]> wrote:
> What compiler errors are you getting?
> -deech
>
> On Fri, Jul 1, 2011 at 12:55 AM, Ruohao Li <[hidden email]> 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
> > [hidden email]
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
> >
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe



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







_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe