Division in Haskell
But every decision for something is a decision against something else.
—H. G. Tannhaus, Dark (2017)

While learning about division in Haskell, I realized that the concept is not as trivial as I initially wanted it to be. There are subtle differences between the functions that can easily trip someone who’s not aware of them. quot performs integer division that rounds towards zero. div is like quot, but it rounds below zero—negative infinity. rem returns the remainder of a division. mod, on the other hand, performs modular arithmetic.
In the GHCi interaction below, quotRem returns a tuple of the application of quot and rem to its arguments, while divMod returns a tuple of the application div and mod to its arguments.
Prelude> quotRem 13 5
(2,3)
Prelude> quotRem (-13) 5
(-2,-3)
Prelude> quotRem 13 (-5)
(-2,3)
Prelude> quotRem 5 13
(0,5)
Prelude> quotRem (-5) 13
(0,-5)
Prelude> quotRem 5 (-13)
(0,5)
Prelude> divMod 13 5
(2,3)
Prelude> divMod (-13) 5
(-3,2)
Prelude> divMod 13 (-5)
(-3,-2)
Prelude> divMod 5 13
(0,5)
Prelude> divMod (-5) 13
(-1,8)
Prelude> divMod 5 (-13)
(-1,-8)
Giving special attention to negative numbers, here are some observations about it:
- quotRemand- divModbehave the same, if all the arguments are positive.
- quotreturns- 0, if the dividend is less than the divisor.
- remfollows the sign of the dividend.
- remreturns the dividend, if the dividend is less than the divisor.
- divrounds off the divisor to the negative infinity, if either the dividend or divisor is negative.
- modfollows the sign of the divisor.
- divreturns- 0, if the dividend is less than the divisor, and both arguments are positive.
- modreturns the dividend, if the dividend is less than the divisor, and both arguments are positive.
- divreturns- -1if the dividend is negative, and its absolute value is less than the divisor.
- quotand- divreturns- 0if the dividend is- 0, and the divisor is not zero.
- modreturns the difference of the absolute values of the dividend and the divisor, following the sign of the divisor, if either the dividend or the divisor is negative, and if the absolute of the dividend is less than the absolute value of the divisor.
- If either the dividend or divisor of modis a negative, and that the absolute value of the dividend is larger than the abvolute value of the divisor,modreturns a value such that when this value is added to the result of thedivof the dividend and divisor, multiplied by the divisor, it returns the dividend ofmod. That is:
 So ifx == (x `mod` y) + (x `div` y) * yx = (-13)andy = 5, then
 evaluates to(-13) == ((-13) `mod` 5) + ((-13) `div` 5) * 5True.