macros - 定义 Lisp 宏时是否使用双反引号(双逗号)?

标签 macros lisp

当一个人定义一个使用 macrolet 的宏,或定义一个定义宏的宏时,似乎有人使用 ,',, 来取消引用。有没有我需要使用 ,, 的情况?

最佳答案

当然可以。

这里是 code来自 Graham's "On Lisp" :

(defmacro =defun (name parms &body body)
  (let ((f (intern (concatenate 'string
                                "=" (symbol-name name)))))
    `(progn
       (defmacro ,name ,parms
         `(,',f *cont* ,,@parms))
       (defun ,f (*cont* ,@parms) ,@body))))

另一个例子来自clx/gcontext.lisp:

(macrolet ((def-gc-internals (name &rest extras)
             (let ((macros nil)
                   (indexes nil)
                   (masks nil)
                   (index 0))
               (dolist (name *gcontext-components*)
                 (push `(defmacro ,(xintern 'gcontext-internal- name) (state)
                          `(svref ,state ,,index))
                       macros)
                 (setf (getf indexes name) index)
                 (push (ash 1 index) masks)
                 (incf index))
               (dolist (extra extras)
                 (push `(defmacro ,(xintern 'gcontext-internal- (first extra)) (state)
                          `(svref ,state ,,index))
                       macros)
                 ;; don't override already correct index entries
                 (unless (or (getf indexes (second extra)) (getf indexes (first extra)))
                   (setf (getf indexes (or (second extra) (first extra))) index))
                 (push (logior (ash 1 index)
                               (if (second extra)
                                   (ash 1 (position (second extra) *gcontext-components*))
                                   0))
                       masks)
                 (incf index))
               `(within-definition (def-gc-internals ,name)
                  ,@(nreverse macros)
                  (eval-when (:execute :compile-toplevel :load-toplevel)
                    (defparameter *gcontext-data-length* ,index)
                    (defparameter *gcontext-indexes* ',indexes)
                    (defparameter *gcontext-masks*
                      ',(coerce (nreverse masks) 'simple-vector)))))))
  (def-gc-internals ignore
    (:clip :clip-mask) (:dash :dashes) (:font-obj :font) (:timestamp)))

简而言之,这不是很常见,但并非闻所未闻。

关于macros - 定义 Lisp 宏时是否使用双反引号(双逗号)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17938242/

相关文章:

C宏给出编译时错误

c++ - 宏不通过直接调用扩展,而是通过间接调用扩展

macros - 我如何在 Clojure 中编写 "defn"宏?

algorithm - 如何将这种递归解决方案转换为迭代解决方案?

arrays - 如何在 LISP 的函数中创建一个大小为参数的数组?

vba - 将BeforeDoubleClick_event的模块代码添加到动态创建的工作表中

c - 大多数嵌入式 C 编译器如何为内存映射 I/O 定义符号?

lisp - 不能在泛型函数方法中使用变量吗? (CLOS/LISP)

Lisp:检查函数确定其所需的参数

macros - 在宏中取消引用时 undefined variable