From ccf44441e732b3194069a45fe1518e5013fc53c8 Mon Sep 17 00:00:00 2001 From: Owen Rodley Date: Sun, 19 May 2013 11:13:28 +1200 Subject: [PATCH] Fix - and / Introduce a new macro, as the one for + and * doesn't handle them correctly --- src/numbers.lisp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/numbers.lisp b/src/numbers.lisp index f49fd23..18d679c 100644 --- a/src/numbers.lisp +++ b/src/numbers.lisp @@ -26,9 +26,22 @@ ,init-sym)))) (define-variadic-op + 0) -(define-variadic-op - 0) (define-variadic-op * 1) -(define-variadic-op / 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)) (defun 1+ (x) (+ x 1)) (defun 1- (x) (- x 1)) -- 1.7.10.4