haskell modulus function using repeated subtractions -
i required write modulus function (using repeated subtractions , not using primitive mod function).
mod' :: int -> int -> int mod' x 0 = 0 mod' 0 x = x mod' x y | x >= y = (mod' (x-y) y) | otherwise = y
i did not working. compiles, incorrect answers this:
*main> 7 `mod'` 4 4 *main> 3 `mod'` 5 5
what wrong?
otherwise = y
this wrong: when x < y
, x mod y == x
.
also, shouldn't x `mod` 0
error?
edit: and, mod' 0 x = 0
, not x.
Comments
Post a Comment