[Small-cl-src] Funcallable macros

Lars Brinkhoff lars at nocrew.org
Tue May 25 10:41:35 UTC 2004


;;; Lisp apprentice's wet dream: funcallable macros.

;;; Probable newbie usage example: (reduce #`and '(t t t nil t t))

;;; Two implementations are provided: one trivial using eval, and one
;;; that memoizes compiled functions.

(defun funcallable-macro (name)
  (lambda (&rest args)
    (eval (cons name args))))

(defun funcallable-macro (name)
  (let ((table (make-hash-table :test #'eql)))
    (lambda (&rest args)
      (let ((n (length args)))
	(apply (or (gethash n table)
		   (setf (gethash n table)
			 (make-funcallable-macro-function name n)))
	       args)))))

(defun make-funcallable-macro-function (name n)
  (let ((args nil))
    (dotimes (i n)
      (push (gensym) args))
    (compile nil `(lambda ,args ,(macroexpand `(,name , at args))))))

(set-dispatch-macro-character #\# #\`
			      (lambda (s c n)
				`(funcallable-macro ',(read s t nil t))))





More information about the Small-cl-src mailing list