Mixed division

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

Mixed division

Galaxy Being
I can divide a float by an integer

>  2.4 / 3
0.7999999999999999

but why can I not do this?

< 2.4 / (length [1,2,3])
<interactive>:288:1-22: error:
    * No instance for (Fractional Int) arising from a use of `/'
    * In the expression: 2.4 / (length [1, 2, 3])

LB

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Mixed division

Ut Primum
Hi,
when you write 3 Haskell sees it as a "num" while the type of the length [1,2,3] is "int". So 3 and length [1,2,3] have not the same type.

Il ven 5 feb 2021, 19:23 Lawrence Bottorff <[hidden email]> ha scritto:
I can divide a float by an integer

>  2.4 / 3
0.7999999999999999

but why can I not do this?

< 2.4 / (length [1,2,3])
<interactive>:288:1-22: error:
    * No instance for (Fractional Int) arising from a use of `/'
    * In the expression: 2.4 / (length [1, 2, 3])

LB
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Mixed division

Galaxy Being
This works

myAvg :: [Float] -> Float
myAvg [] = 0.0
myAvg xs = (foldr (+) 0.0 xs) / (fromIntegral (length xs) :: Float)

but is there a simpler way to "cast" the value of (length xs)?

On Fri, Feb 5, 2021 at 12:29 PM Ut Primum <[hidden email]> wrote:
Hi,
when you write 3 Haskell sees it as a "num" while the type of the length [1,2,3] is "int". So 3 and length [1,2,3] have not the same type.

Il ven 5 feb 2021, 19:23 Lawrence Bottorff <[hidden email]> ha scritto:
I can divide a float by an integer

>  2.4 / 3
0.7999999999999999

but why can I not do this?

< 2.4 / (length [1,2,3])
<interactive>:288:1-22: error:
    * No instance for (Fractional Int) arising from a use of `/'
    * In the expression: 2.4 / (length [1, 2, 3])

LB
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners