# Fractional Int Classic List Threaded 6 messages Open this post in threaded view
|

## Fractional Int

 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
Open this post in threaded view
|

 Sorry, forgot to reply to all. ---------- Forwarded message ---------- From: Sean Bartell <[hidden email]> Date: Fri, Mar 20, 2009 at 5:58 PM Subject: Re: [Haskell-beginners] Fractional Int To: Zachary Turner <[hidden email]> 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 <[hidden email]> > > 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 > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners>
Open this post in threaded view
|

## Fractional Int

 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] [hidden email] system administrator [openafs,heimdal,too many hats] [hidden email] electrical and computer engineering, carnegie mellon university    KF8NH -------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 195 bytes Desc: This is a digitally signed message part Url : http://www.haskell.org/pipermail/beginners/attachments/20090320/82dd6ffd/PGP.bin
Open this post in threaded view
|

## Fractional Int

 2009/3/20 Brandon S. Allbery KF8NH <[hidden email]> > 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? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090320/20c3543b/attachment.htm