Skip to content

Define even using mod instead of rem

Summary

even and odd are defined in GHC.Real as

even, odd       :: (Integral a) => a -> Bool
even n          =  n `rem` 2 == 0
odd             =  not . even

For an Int, which is probably the most common use case, even compiles to remInt# x 2# in core and a bunch of instructions in asm.
If we use mod instead, it compiles to andI# x 1# which is far simpler.
Haskell playground link: https://play.haskell.org/saved/m7xZGpaS

So, should even be defined using mod instead? Is rem better for any common non-Int integral types?

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information