[Haskell-beginners] Fractional Int

Zachary Turner divisortheory at gmail.com
Fri Mar 20 16:51:11 EDT 2009


Why is there no instance of Fractional Int or Fractional Integer?  Obviously
integers are fractions with denominator 1.  I was just doing some basic
stuff to get more familiar with Haskell, and was seriously racking my brain
trying to figure out why the following wouldn't work:

intToString :: Int -> [Char]
intToString n | n<10 = chr (n + (ord '0')):[]
intToString n =
    let q = truncate (n/10)
        r = n `mod` 10
        o = ord '0'
        ch = chr (r + o)
    in ch:(intToString q)

(yes, this ends up converting the string in reverse, but that's another
issue :P)

I later realized that I could use members of the Integral typeclass such as
divMod, mod, etc to make this better, but nonetheless, why should
truncate(n/10) be invalid, when n is an Int?  changing it to
truncate((toRational n)/10) works, but I would expect Integers to already be
rational.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090320/0a0afdb1/attachment.htm


More information about the Beginners mailing list