2

我目前正在通过 Graham 的书“ANSI Common Lisp”学习 lisp,作为练习,我正在编写基于儒略日的日历计算。如您所知,复活节星期日每年都在变化,还有大约 10 个其他特殊日子,其实际日期取决于复活节星期日的日期。

我想为这些日子的每一天定义一个函数,所有这些都遵循以下模式:

(defun carnaval (year)
  "Carnaval Monday of YEAR.

This is 48 days before Easter Sunday."
  (- (easter year) 48))

与其重复 10 次类似的声明,不如使用宏:

(defmacro %defeasterday (function-name screen-name offset)
  `(defun ,function-name (year)
     ,(format nil "~A of YEAR.~%~%This is ~A day~:p ~A Easter Sunday"
              screen-name
              (abs offset)
              (if (< 0 offset) "after" "before"))
     (+ (easter year) ,offset)))

(开始%标志着我不打算在定义宏的包中导出宏。)

该宏可用于为日期基于东部星期日日期的每一天定义一个函数:

(%defeasterday carnaval   "Carnaval Monday" -48)
(%defeasterday mardi-gras "Mardi gras"      -47)
(%defeasterday ash        "Ash Wednesday"   -46)
…

现在,为了练习,我想将所有数据打包到一个列表中,并%defeasterday在其项目上使用宏。我的尝试是

(mapc #'(lambda (args) (apply #'%defeasterday args))
      '((carneval   "Carneval Monday" -48)
        (mardi-gras "Mardi gras"      -47)
        (ash        "Ash Wednesday"   -46)))

这失败了

Execution of a form compiled with errors.
Form:
  #'%DEFEASTERDAY
Compile-time error:
  The :macro name %DEFEASTERDAY was found as the argument to FUNCTION.
   [Condition of type SB-INT:COMPILED-PROGRAM-ERROR]

这告诉我宏不仅仅是“将代码映射到代码的函数”,因为apply它对运行它们很挑剔。

如何使用%defeasterday上面的宏来迭代列表?

easter(如果您出于测试目的需要临时功能,请(defun easter () 2457860)给出 2017 年的预期答案。)

4

1 回答 1

6

应用宏不起作用

您不能应用宏:

(apply #'defsomething '(foo bar))

但是你可以评估一个表格:

(eval (let ((args '(foo bar)))
         `(defsomething ,@args)))

或者

(let ((args '(foo bar)))
  (eval `(defsomething ,@args)))

COMPILE如果您想确保代码已编译,另请参阅函数。

使用定义宏

使用定义(!)宏的正确方法是:

(%defeasterdays
  (carnaval   "Carnaval Monday" -48)
  (mardi-gras "Mardi gras"      -47)
  (ash        "Ash Wednesday"   -46))

然后%defeasterdays宏应该在上面扩展为:

(progn
 (%defeasterday carnaval   "Carnaval Monday" -48)
 (%defeasterday mardi-gras "Mardi gras"      -47)
 (%defeasterday ash        "Ash Wednesday"   -46))

DEFUN是一个顶级宏。曾经通常想保持这种方式。如果您使用EVAL表单DEFUN,则它不是文件编译器的顶层。因此,您需要在宏中进行转换,以使定义形式仍处于“顶层”。PROGN对于文件编译器,子表单仍处于顶层。

让文件编译器满意

您可以使用文件编译器来编译以下代码:

; we need the file compiler to know the value of *DAYS*
;  thus the eval-when. By default `DEFVAR` would not have
;  been executed
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defvar *days*
    '((carnaval   "Carnaval Monday" -48)
      (mardi-gras "Mardi gras"      -47)
      (ash        "Ash Wednesday"   -46))))

; a file compiler sees the following macro
:  and its definition is automatically available at compile time
(defmacro %defeasterday (function-name screen-name offset)
  `(defun ,function-name (year)
     ,(format nil "~A of YEAR.~%~%This is ~A day~:p ~A Easter Sunday"
              screen-name
              (abs offset)
              (if (< 0 offset) "after" "before"))
     (+ (easter year) ,offset)))

; same here, the compiler learns about the next macro
(defmacro %defeasterdays (list)
  `(progn ,@(loop for item in (symbol-value list)
                  collect `(%defeasterday ,@item))))

; now the file compiler sees the following form.
;  it will be macro expanded. The macros are known.
;  one of the macros now needs at expansion time the value
;  of *DAYS*. Since we have made *DAYS* known to the compiler,
;  this will work.
(%defeasterdays *days*)

主要优点是文件编译器将在编译时看到所有生成函数的定义。它将能够为函数生成有效的代码,也可以为调用这些函数的表单生成有效的代码。

您也可以加载此文件,但这取决于代码是否会被编译或者是否最终得到解释函数的实现。

于 2017-09-29T13:54:45.507 回答