Fwd: [Haskell-beginners] Fractional Int

Sean Bartell wingedtachikoma at gmail.com
Fri Mar 20 18:01:22 EDT 2009

Sorry, forgot to reply to all.

---------- Forwarded message ----------
From: Sean Bartell <wingedtachikoma at gmail.com>
Date: Fri, Mar 20, 2009 at 5:58 PM
Subject: Re: [Haskell-beginners] Fractional Int
To: Zachary Turner <divisortheory at gmail.com>

For a type "a" to be Fractional requires there to be:
(/) :: a -> a -> a
You can't divide an Int by another Int and (in general) get a third
Int. You would probably want something like a "Fractionable"
typeclass, with
(/) :: a -> a -> b
which would result in a Rational, but Haskell doesn't have this.

2009/3/20 Zachary Turner <divisortheory at gmail.com>
> 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.
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

More information about the Beginners mailing list