On Feb 10, 2009, at 4:15 PM, Chouser wrote: > > > (defn mod42 > "Modulus of num and div. Truncates toward negative infinity." > [num div] > (if-not (and (integer? num) (integer? div)) > (throw (IllegalArgumentException. "mod requires two integers")) > (let [m (rem num div)] > (if (or (zero? m) (> (* num div) 0)) > m > (+ m div))))) >
Just for giggles, here's how SBCL (Steel Bank Common Lisp) defines the two functions: (defun rem (number divisor) #!+sb-doc "Return second result of TRUNCATE." (multiple-value-bind (tru rem) (truncate number divisor) (declare (ignore tru)) rem)) (defun mod (number divisor) #!+sb-doc "Return second result of FLOOR." (let ((rem (rem number divisor))) (if (and (not (zerop rem)) (if (minusp divisor) (plusp number) (minusp number))) (+ rem divisor) rem))) Notice that despite the documentation, MOD does not actually call FLOOR. It indirectly calls TRUNCATE via REM. It's also important to notice that Common Lisp is a Lisp-2, so that we can store the result of the REM call in a variable with the same name in the LET form. Aloha, David Sletten --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---