code_junkie

When is the difference between quotRem and divMod useful?

Nov 12th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. (x `quot` y)*y + (x `rem` y) == x
  2. (x `div` y)*y + (x `mod` y) == x
  3.  
  4. Prelude> (-12) `quot` 5
  5. -2
  6. Prelude> (-12) `div` 5
  7. -3
  8.  
  9. (-11)/5 = -2
  10. (-11)%5 = -1
  11. 5*((-11)/5) + (-11)%5 = 5*(-2) + (-1) = -11.
  12.  
  13. (-11)/5 = -3
  14. (-11)%5 = 4
  15. 5*((-11)/5) + (-11)%5 = 5*(-3) + 4 = -11.
  16.  
  17. let buggyOdd x = x `rem` 2 == 1
  18. buggyOdd 1 // True
  19. buggyOdd (-1) // False (wrong!)
  20.  
  21. let odd x = x `mod` 2 == 1
  22. odd 1 // True
  23. odd (-1) // True
  24.  
  25. let odd x = x `rem` 2 /= 0
  26. odd 1 // True
  27. odd (-1) // True
Add Comment
Please, Sign In to add comment