
Sorry, forgot to reply to all.
---------- Forwarded message ----------
From: Sean Bartell
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 2009 Mar 20, at 18:01, Sean Bartell wrote:
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.
...but there is (%) :: (Integral a) => a -> a -> Ratio a -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

2009/3/20 Brandon S. Allbery KF8NH
On 2009 Mar 20, at 18:01, Sean Bartell wrote:
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.
...but there is (%) :: (Integral a) => a -> a -> Ratio a
Thanks, I knew about % but didn't remember about it when I was working on this sample :) So that being said, consider the following: Prelude Data.Ratio> let x = 5::Int Prelude Data.Ratio> :t x x :: Int Prelude Data.Ratio> :t (x%3) (x%3) :: Ratio Int Prelude Data.Ratio> let y = truncate (x%3) Prelude Data.Ratio> :t y y :: Integer Why does y now have the type Integer instead of Int?

On 2009 Mar 20, at 20:07, Zachary Turner wrote:
2009/3/20 Brandon S. Allbery KF8NH
...but there is (%) :: (Integral a) => a -> a -> Ratio a Prelude Data.Ratio> let y = truncate (x%3) Prelude Data.Ratio> :t y y :: Integer
Why does y now have the type Integer instead of Int?
Because of the monomorphism restriction and defaulting. Actually use it somewhere and it will take the appropriate (Integral) type. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On 2009 Mar 20, at 20:14, Brandon S. Allbery KF8NH wrote:
On 2009 Mar 20, at 20:07, Zachary Turner wrote:
2009/3/20 Brandon S. Allbery KF8NH
...but there is (%) :: (Integral a) => a -> a -> Ratio a Prelude Data.Ratio> let y = truncate (x%3) Prelude Data.Ratio> :t y y :: Integer
Why does y now have the type Integer instead of Int?
Because of the monomorphism restriction and defaulting. Actually use it somewhere and it will take the appropriate (Integral) type.
uh, clarification: because y has no arguments, the monomorphism restriction comes into play; if you define y as above in a program it will also end up defaulting to Integer. The expression "truncate (x %3)", on the other hand, is of type "Integral a => a" (as long as it's not placed into a context that triggers monomorphism). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (3)
-
Brandon S. Allbery KF8NH
-
Sean Bartell
-
Zachary Turner