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