Skip to content

Instantly share code, notes, and snippets.

@5outh
Created August 15, 2012 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5outh/3364121 to your computer and use it in GitHub Desktop.
Save 5outh/3364121 to your computer and use it in GitHub Desktop.
FracNum
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