In the code I posted the let evaluated rem before checking the
arguments.
It would be better arranged like so:
(defn mod2
"Modulus of num and div. Truncates toward negative infinity."
[num div]
(if (or (not (integer? num)) (not (integer? div)))
(throw (IllegalArgumentException. "mod requires two integers"))
(let [m (rem num div)]
(if (and
(not (zero? m))
(or (< num 0 div) (< div 0 num)))
(+ m div)
m))))
Same behavior:
user=> (map #(mod2 % 3) (range -9 9))
(0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2)
user=> (map #(mod2 % -3) (range -9 9))
(0 -2 -1 0 -2 -1 0 -2 -1 0 -2 -1 0 -2 -1 0 -2 -1)
I'm sure there are better ways still, just wanted to correct my
mistake.
Regards,
Tim.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---