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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -