[Haskell-beginners] Fractional Int

Zachary Turner divisortheory at gmail.com
Fri Mar 20 20:07:45 EDT 2009


2009/3/20 Brandon S. Allbery KF8NH <allbery at ece.cmu.edu>

> 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


More information about the Beginners mailing list