From: Owen Rodley Date: Sat, 18 May 2013 13:59:08 +0000 (+1200) Subject: Variadic functions for * and / X-Git-Url: http://repo.macrolet.net/gitweb/?a=commitdiff_plain;h=03fcd04876ff098055a8313d65fb1bef7bb57a5d;p=jscl.git Variadic functions for * and / Uses a new macro DEFINE-VARIADIC-OP, which also captures a pattern used in + and -, and is used for them as well --- diff --git a/src/numbers.lisp b/src/numbers.lisp index 060e657..a3105bb 100644 --- a/src/numbers.lisp +++ b/src/numbers.lisp @@ -15,24 +15,24 @@ ;;;; Various numeric functions and constants -;; Basic functions -(defun * (x y) (* x y)) -(defun / (x y) (/ x y)) +;; 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)))) + +(define-variadic-op + 0) +(define-variadic-op - 0) +(define-variadic-op * 1) +(define-variadic-op / 1) + (defun 1+ (x) (+ x 1)) (defun 1- (x) (- x 1)) -(defun + (&rest args) - (let ((r 0)) - (dolist (x args r) - (incf r x)))) - -(defun - (x &rest others) - (if (null others) - (- x) - (let ((r x)) - (dolist (y others r) - (decf r y))))) - (defun truncate (x &optional (y 1)) (floor (/ x y)))