Advertisement
Guest User

RatOps(1).hs

a guest
Dec 6th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. module RatOps where
  2. type Rat = (z :: Int,n :: Int)
  3.  
  4. asop :: Num z => (z -> z -> z) -> (z,z) -> (z,z) -> (z,z)
  5. asop op (z1,n1)(z2,n2) = ((z1*n2) `op` (z2*n1), (n1*n2))
  6.  
  7. short :: Num z => (z -> z) -> (z,z) -> (z,z)
  8. short (z1,n1) = ((z1/gcd z1 n1), (gcd z1 n1)
  9.  
  10. negn :: Rat -> Bool
  11. negn x = if n x = 0
  12. then True
  13. else False
  14.  
  15. negz :: Rat -> Bool
  16. negz x = if z x = 0
  17. then True
  18. else False
  19.  
  20. addR :: Rat -> Rat -> Rat
  21. addR x y|negz x = True || (n y == 1) = (0, 1)
  22. |negn y = True || (z x == 0) = (0, 0)
  23. |otherwise = short $ asop(+) x y
  24.  
  25. subR :: Rat -> Rat -> Rat
  26. subR x y|negz x = True || (n y == 1) = (0, 1)
  27. |negn y = True || (z x == 0) = (0, 0)
  28. |otherwise = short $ asop(-) x y
  29.  
  30. mulR :: Rat -> Rat -> Rat
  31. mulR x y|negz x = True || (n y == 1) = (0, 1)
  32. |negn y = True || (z x == 0) = (0, 0)
  33. |otherwise = short ((z x * z y),(n x * n y)
  34.  
  35. divR :: Rat -> Rat -> Rat
  36. divR x y|negz x = True || (n y == 1) = (0, 1)
  37. |negn y = True || (z x == 0) = (0, 0)
  38. |otherwise = short $ x `mulR` (switch y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement