Lisp - 将未引用的列表传递给宏

Lisp - Passing unquoted list to macro

我目前正在 Lisp 中试验宏,我想编写一个可以处理语法如下的宏:

(my-macro (args1) (args2))

该宏应该包含两个列表,然后可以在我的宏中进行进一步处理。然而,要注意的是,列表未被引用以模仿某些真实 Lisp/CLOS 函数的语法。这可能吗?

目前,我在尝试执行类似操作时遇到以下错误:

Undefined function ARGS1 called with arguments ().

提前致谢!

我认为你需要展示你尝试过的事情。这是一个(愚蠢的)宏的示例,它的参数模式与您的几乎相同:

(defmacro stupid-let ((&rest vars) (&rest values) &body forms)
  ;; Like LET but with a terrible syntax
  (unless (= (length vars) (length values))
    (error "need exactly one value for each variable"))
  (unless (every #'symbolp vars)
    (error "not every variable is a symbol"))
  `(let ,(mapcar #'list vars values) ,@forms))

然后

> (macroexpand '(stupid-let (a b c) (1 2 3) (+ a b c)))
(let ((a 1) (b 2) (c 3)) (+ a b c))

上面的宏依赖于 defmacro 的 arglist-destructuring,但你不必那样做:

(defun proper-list-p (l)
  ;; elaborate version with an occurs check, quadratic.
  (labels ((plp (tail tails)
             (if (member tail tails)
                 nil
               (typecase tail
                 (null t)
                 (cons (plp (rest tail) (cons tail tails)))
                 (t nil)))))
    (plp l '())))

(defmacro stupid-let (vars values &body forms)
  ;; Like LET but with a terrible syntax
  (unless (and (proper-list-p vars) (proper-list-p values))
    (error "need lists of variables and values"))
  (unless (= (length vars) (length values))
    (error "need exactly one value for each variable"))
  (unless (every #'symbolp vars)
    (error "not every variable is a symbol"))
  `(let ,(mapcar #'list vars values) ,@forms))

作为一个更有用的示例,这里有一个有点像 CLOS with-slots / with-accessors 宏的宏:

(defmacro with-mindless-accessors ((&rest accessor-specifications) thing
                                   &body forms)
  "Use SYMBOL-MACROLET to define mindless accessors for THING.

Each accessor specification is either a symbol which names the symbol
macro and the accessor, or a list (macroname accessorname) which binds
macroname to a symbol macro which calls accessornam.  THING is
evaluated once only."
  (multiple-value-bind (accessors functions)
      (loop for accessor-specification in accessor-specifications
            if (symbolp accessor-specification)
            collect accessor-specification into acs
            and collect accessor-specification into fns
            else if (and (proper-list-p accessor-specification)
                         (= (length accessor-specification) 2)
                         (every #'symbolp accessor-specification))
            collect (first accessor-specification) into acs
            and collect (second accessor-specification) into fns
            else do (error "bad accessor specification ~A" accessor-specification)
            end
            finally (return (values acs fns)))
    (let ((thingn (make-symbol "THING")))
    `(let ((,thingn ,thing))
       (symbol-macrolet ,(loop for accessor in accessors
                               for function in functions
                               collect `(,accessor (,function ,thingn)))
         ,@forms)))))

所以现在我们可以写这个有点无用的代码了:

> (with-mindless-accessors (car cdr) (cons 1 2)
    (setf cdr 3)
    (+ car cdr))
4

还有这个:

> (let ((l (list 1 2)))
    (with-mindless-accessors (second) l
      (setf second 4)
      l))
(1 4)