Created
August 15, 2012 22:06
-
-
Save 5outh/3364121 to your computer and use it in GitHub Desktop.
FracNum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
instance Num Fraction where | |
(-) f f' = f + (negate f') | |
(+) (Frac a b) (Frac c d) = Frac num denom | |
where denom = lcm b d | |
num = a * (denom `quot` b) + c * (denom `quot` d) | |
(*) (Frac a b) (Frac c d) = Frac (a*c) (b*d) | |
negate (Frac a b) = Frac (-a) b | |
abs f = fmapF abs f | |
where fmapF f (Frac a b) = Frac (f a) (f b) | |
fromInteger x = Frac x 1 | |
signum (Frac a b) = if a == 0 then 0 | |
else if b > 0 then | |
if a < 0 then (-1) | |
else 1 | |
else if a < 0 then 1 | |
else (-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment