purely-functionalclean-language

Modulo function in Clean


Is there a predefined way to compute the modulo of two integers in Clean?

StdOverloaded defines the (mod) typeclass, but StdInt does not contain an instance of it, and StdEnv does not either anywhere else. I have seen a language overview about Clean version 2.0, which mentions StdInt's mod instance, but in 2.4, it is definitely missing.


Solution

  • StdInt delivers another alternative: it's called rem. Works just like the old fashioned mod function, but also works fine with signed integers.

    10 rem 7 // 3
    
    10 rem -7 // 3
    
    -10 rem 7 // -3
    
    -10 rem -7 //-3