[Haskell-cafe] Re: Simple quirk in behavior of `mod`

Chris Kuklewicz haskell at list.mightyreason.com
Wed Jul 22 04:05:33 EDT 2009

Nathan Bloomfield wrote:
> Hello haskell-cafe;
> I'm fiddling with this
> <http://cdsmith.wordpress.com/2009/07/20/calculating-multiplicative-inverses-in-modular-arithmetic/>
> blog post about inverting elements of Z/(p), trying to write the
> inversion function in pointfree style. This led me to try executing
> statements like
>    n `mod` 0
> which in the ring theoretic sense should be n, at least for integers*.
> (MathWorld agrees. <http://mathworld.wolfram.com/Congruence.html>)

I agree that (n `mod` 0) ought to be n.  Specifically

divMod n 0 = (0,n)


quotRem n 0 = (0,n)

In (divMod n m) the sign of the remainder is always the same as the sign
of m, unless n or m is zero.  In (quotRem n m) the sign of the quotient
is the product of the signs of n and m, unless n or m is zero.


More information about the Haskell-Cafe mailing list