
Hello there! Although you might not believe it since I'm going to ask a very very very basic question, I have been using Haskell for educational purposes for a couple semesters now. Nevertheless, I don't think I have ever worked with non-Int number types! :) I don't understand why I get this error. I mean, apparently "/" is not defined for integers but I don't know how to "cast" the result of the length function into a Double... Prelude> (length [1,2]) / 3 <interactive>:1: No instance for (Fractional Int) arising from use of `/' at <interactive>:1 In the definition of `it': it = (length [1, 2]) / 3 Besides, a simple integer division works: Prelude> 2 / 3 0.6666666666666666 But I would guess that's because '2' and '3' are parsed as Floats because they are used in a divide function. Any help will be appreciated! Best regards, - Matías

Hello there!
Although you might not believe it since I'm going to ask a very very very basic question, I have been using Haskell for educational purposes for a couple semesters now. Nevertheless, I don't think I have ever worked with non-Int number types! :)
I don't understand why I get this error. I mean, apparently "/" is not defined for integers but I don't know how to "cast" the result of the length function into a Double...
fromInteger or fromIntegral. (the former converts an Integer to any numeric type, the latter converts any integral type to any numeric type). --KW 8-)

I don't understand why I get this error. I mean, apparently "/" is not defined for integers but I don't know how to "cast" the result of the length function into a Double...
Use div for dividing integers with loss of precision: 3 `div` 2 (or: div 3 2) == 1 "Casting" an Int to a Float/Double can be done using fromIntegral: fromIntegral (length [1,2]) / 3 == 0.6666666666666666
Besides, a simple integer division works:
Prelude> 2 / 3 0.6666666666666666
This is not an integer division. Writing down a literal (like 2 or 3) denotes a number, not necessarily an Int. The context will influence what type it will be. Something I find really confusing myself, too (which is why it is not in Helium :-). Greetings, Arjan

"Matias Hernandez"
I mean, apparently "/" is not defined for integers but I don't know how to "cast" the result of the length function into a Double...
Prelude> (length [1,2]) / 3
Prelude> fromIntegral (length [1,2])/3 0.666666666666667
Prelude> 2 / 3 0.6666666666666666
Literal 2 means fromIntegral 2, but length::[a]->Int. -- Feri.

On 2004-03-18 at 15:08+0100 Ferenc Wagner wrote:
"Matias Hernandez"
writes: I mean, apparently "/" is not defined for integers but I don't know how to "cast" the result of the length function into a Double...
Prelude> (length [1,2]) / 3
Literal 2 means fromIntegral 2, but length::[a]->Int.
Note also Data.List.genericLength :: Num a => [b] -> a -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk
participants (5)
-
Arjan van IJzendoorn
-
Ferenc Wagner
-
Jon Fairbairn
-
Keith Wansbrough
-
Matias Hernandez