SOE ex.4.2


intToFloat :: Int -> Float
intToFloat n = fromInteger (toInteger n)

pixelToInch :: Int -> Float
pixelToInch n = intToFloat n / 100

{-

pixelToInch' :: Int -> Float
pixelToInch' n = intToFloat (n / 100)

The definition of pixelToInch' will fail with error:
Instance of Fractional Int required for definition of pixelToInch'

Other languages have kind of "integer division",
for example 7/2 = 3 (the result is always integer)

So, this will have the same problem as ex.4.1: "lost precision".

BTW. Tried this:

x :: Int
x = 20

y :: Int
y = 100

:t (x/y)
ERROR - Cannot infer instance
*** Instance   : Fractional Int
*** Expression : x / y

hmm....
:t (/)
(/) :: Fractional a => a -> a -> a

I will study Haskell's numeric type classes later to understand what happend.

-}

Leave a Reply