您可能知道,Lisp 复合表单通常是从外向内处理的。您必须查看最外层嵌套的第一个位置的符号才能理解表单。那个符号完全决定了形式的意义。以下表达式都包含(b c),含义完全不同;因此,我们无法通过首先分析(b c) 部分来理解它们:
;; Common Lisp: define a class A derived from B and C
(defclass a (b c) ())
;; Common Lisp: define a function of two arguments
(defun a (b c) ())
;; add A to the result of calling function B on variable C:
(+ a (b c))
传统上,Lisp 方言将表单分为运算符表单和函数调用表单。运算符形式具有完全任意的含义,由编译或解释该函数的代码段确定(例如,评估简单地递归所有函数调用的参数形式,并将结果值传递给函数)。
从早期的历史开始,Lisp 就允许用户编写自己的运算符。存在两种方法:解释运算符(历史上称为fexprs)和编译运算符称为宏。两者都围绕着接收未评估形式作为参数的函数的想法,以便它可以实现自定义策略,从而用新的行为扩展评估模型。
fexpr 类型操作符在运行时被简单地传递给表单,以及一个环境对象,它可以使用它来查找变量的值等。然后该运算符遍历表单并实现行为。
宏操作符在宏扩展时被传递给表单(这通常发生在读取顶级表单时,就在它们被评估或编译之前)。它的工作不是解释表单的行为,而是通过生成代码来翻译它。 IE。宏是一个迷你编译器。 (生成的代码可以包含更多的宏调用;宏扩展器会处理这些,确保所有宏调用都被抽取。)
fexpr 方法失宠,很可能是因为它效率低下。它基本上使编译变得不可能,而 Lisp 黑客则重视编译。 (Lisp 早在 1960 年左右就已经是一种编译语言了。)fexpr 方法也对词汇环境充满敌意。它需要fexpr,它是一个函数,能够窥视到它被调用的表单的变量绑定环境,这是一种词法范围不允许的封装违规。
宏编写稍微困难一些,并且在某些方面不如 fexprs 灵活,但是从 1960 到 70 年代,Lisp 对宏编写的支持得到了改进,使其尽可能容易。宏最初接收整个表单,然后必须自己解析它。宏定义系统发展成为一种为宏函数提供参数的东西,这些参数以易于理解的部分接收分解的语法,包括语法的一些嵌套方面。还开发了用于编写代码模板的反引号语法,使得代码生成更容易表达。
所以回答你的问题,我自己怎么写这样的表格?例如,如果:
;; Imitation of old-fashioned technique: receive the whole form,
;; extract parts from it and return the translation.
;; Common Lisp defmacro supports this via the &whole keyword
;; in macro lambda lists which lets us have access to the whole form.
;;
;; (Because we are using defmacro, we need to declare arguments "an co &optional al",
;; to make this a three argument macro with an optional third argument, but
;; we don't use those arguments. In ancient lisps, they would not appear:
;; a macro would be a one-argument function, and would have to check the number
;; of arguments itself, to flag bad syntax like (my-if 42) or (my-if).)
;;
(defmacro my-if (&whole if-form an co &optional al)
(let ((antecedent (second if-form)) ;; extract pieces ourselves
(consequent (third if-form)) ;; from whole (my-if ...) form
(alternative (fourth if-form)))
(list 'cond (list antecedent consequent) (list t alternative))))
;; "Modern" version. Use the parsed arguments, and also take advantage of
;; backquote syntax to write the COND with a syntax that looks like the code.
(defmacro my-if (antecedent consequent &optional alternative)
`(cond (,antecedent ,consequent) (t ,alternative))))
这是一个合适的例子,因为最初 Lisp 只有cond。 McCarthy 的 Lisp 中没有 if。那个“语法糖”是后来发明的,可能是一个扩展为cond的宏,就像上面的my-if一样。