X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fnumbers.lisp;h=f5dee9aeb1ecd43489c440a7a22dbf61a0c2ab86;hb=95984c591c75b8085adde1d478b224c2ed29eaa5;hp=66c2db883c5a27c161242e21fb0a9637421bc155;hpb=7a9044854508f83516de80139dad178aefe18e27;p=jscl.git diff --git a/src/numbers.lisp b/src/numbers.lisp index 66c2db8..f5dee9a 100644 --- a/src/numbers.lisp +++ b/src/numbers.lisp @@ -13,35 +13,34 @@ ;; You should have received a copy of the GNU General Public License ;; along with JSCL. If not, see . -;;;; Various numeric functions and constants +(/debug "loading numbers.lisp!") -;; TODO: Use MACROLET when it exists -(defmacro define-variadic-op (operator initial-value) - (let ((init-sym (gensym)) - (dolist-sym (gensym))) - `(defun ,operator (&rest args) - (let ((,init-sym ,initial-value)) - (dolist (,dolist-sym args) - (setq ,init-sym (,operator ,init-sym ,dolist-sym))) - ,init-sym)))) +;;;; Various numeric functions and constants -(define-variadic-op + 0) -(define-variadic-op * 1) +(macrolet ((def (operator initial-value) + (let ((init-sym (gensym)) + (dolist-sym (gensym))) + `(defun ,operator (&rest args) + (let ((,init-sym ,initial-value)) + (dolist (,dolist-sym args) + (setq ,init-sym (,operator ,init-sym ,dolist-sym))) + ,init-sym))))) + (def + 0) + (def * 1)) ;; - and / work differently from the above macro. ;; If only one arg is given, it negates it or takes its reciprocal. ;; Otherwise all the other args are subtracted from or divided by it. -;; TODO: Use MACROLET when it exists -(defmacro define-sub-or-div (operator unary-form) - `(defun ,operator (x &rest args) - (cond - ((null args) ,unary-form) - (t (dolist (y args) - (setq x (,operator x y))) - x)))) - -(define-sub-or-div - (- x)) -(define-sub-or-div / (/ 1 x)) +(macrolet ((def (operator unary-form) + `(defun ,operator (x &rest args) + (cond + ((null args) ,unary-form) + (t (dolist (y args) + (setq x (,operator x y))) + x))))) + (def - (- x)) + (def / (/ 1 x))) + (defun 1+ (x) (+ x 1)) (defun 1- (x) (- x 1)) @@ -62,33 +61,33 @@ (defun signum (x) (if (zerop x) x (/ x (abs x)))) -;; TODO: Use MACROLET when it exists -(defmacro defcomparison (operator) - `(defun ,operator (x &rest args) - (dolist (y args) - (if (,operator x y) - (setq x (car args)) - (return-from ,operator nil))) - t)) - -(defcomparison >) -(defcomparison >=) -(defcomparison =) -(defcomparison <) -(defcomparison <=) -(defcomparison /=) +(macrolet ((def (operator) + `(defun ,operator (x &rest args) + (dolist (y args) + (if (,operator x y) + (setq x (car args)) + (return-from ,operator nil))) + t))) + (def >) + (def >=) + (def =) + (def <) + (def <=) + (def /=)) (defconstant pi 3.141592653589793) (defun evenp (x) (= (mod x 2) 0)) (defun oddp (x) (not (evenp x))) -(flet ((%max-min (x xs func) - (dolist (y xs) - (setq x (if (funcall func x (car xs)) x y))) - x)) - (defun max (x &rest xs) (%max-min x xs #'>)) - (defun min (x &rest xs) (%max-min x xs #'<))) +(macrolet ((def (name comparison) + `(defun ,name (x &rest xs) + (dolist (y xs) + (when (,comparison y x) + (setq x y))) + x))) + (def max >) + (def min <)) (defun abs (x) (if (> x 0) x (- x)))