Haskell loves to adhere to the mathematically acceptable meaning of operators. / should be the opposite of multiplication, but, for example, 5 / 4 * 4 cannot give 5 for an instance of Fractional Integer 1 .
So, if you really want to do truncated integer division, the language forces you 2 to make it explicit with a div or quot . OTOH, if you really want to get the result as a fraction, you can use / fine, but first you need to convert it to a type with a Fractional instance. For instance,
Prelude> let x = 5
Prelude>: tx
x :: Integer
Prelude> let y = fromIntegral x / 100
Prelude> y
5.0E-2
Prelude>: ty
y :: Double
Note that GHCi chose the Double instance here because it is just the default; you can also do
Prelude> let y '= fromIntegral x / 100 :: Rational
Prelude> y '
1% 20
1 Strictly speaking, this inverse identity is not entirely true for the Double instance, either because of floating point smoothing, but there it is true at least approximately.
2 Actually, not a language, but standard libraries. You can define
instance Fractional Integer where (/) = div
then your source code will work fine. Only, this is a bad idea!
source share