Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module RatOps where
- type Rat = (Int,Int)
- asop :: Num z => (z -> z -> z) -> (z,z) -> (z,z) -> (z,z)
- asop op (z1,n1)(z2,n2) = ((z1*n2) `op` (z2*n1), (n1*n2))
- short :: Num z => (z -> z) -> (z,z) -> (z,z)
- short (z1,n1) = ((z1/gcd z1 n1), (gcd z1 n1)
- negn :: Rat -> Bool
- negn (z1,n1) = if n1 = 0
- then True
- else False
- negz :: Rat -> Bool
- negz (z1,n1) = if z1 = 0
- then True
- else False
- switch :: Rat -> Rat
- switch (z,n) = (n,z)
- addR :: Rat -> Rat -> Rat
- addR x y|negz x = True || (snd y == 1) = (0, 1)
- |negn y = True || (fst x == 0) = (0, 0)
- |otherwise = short $ asop(+) x y
- subR :: Rat -> Rat -> Rat
- subR x y|negz x = True || (snd y == 1) = (0, 1)
- |negn y = True || (fst x == 0) = (0, 0)
- |otherwise = short $ asop(-) x y
- mulR :: Rat -> Rat -> Rat
- mulR x y|negz x = True || (snd y == 1) = (0, 1)
- |negn y = True || (fst x == 0) = (0, 0)
- |otherwise = short ((fst x * fst y),(snd x * snd y)
- divR :: Rat -> Rat -> Rat
- divR x y|negz x = True || (snd y == 1) = (0, 1)
- |negn y = True || (fst x == 0) = (0, 0)
- |otherwise = short $ x `mulR` (switch y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement