描述 LISP 宏

问题描述

我已经使用 CLISP 一段时间了,我想说,我终于“理解”了宏。现在我有一个问题: 有没有类似的东西可以为函数而不是宏来描述? 我想在我的宏中放置一个文档字符串,以便有人可以在我的宏上使用“describe-esk”函数/宏来查看它。 如果这还不够清楚,也许这个代码示例会为您清除:

(defmacro while (test &body body)
  "sexier while loop - usage: (while (< x 10) (princ x) (setf x (+ x 1)))"
  (list 'loop 'while test 'do (cons 'progn body)))

(describe-macro 'while)
#=>sexier while loop - usage: (while (< x 10) (princ x) (setf x (+ x 1)))
#=>args: test,body

非常感谢。

解决方法

使用 documentation 函数:

(documentation 'while 'function)

function 类型用于在表达式的函数位置中使用的任何名称,因此它用于函数、宏和特殊运算符。

,

(documentation <x> 'function) 将为您提供 <x> 作为函数或宏的文档:

> (documentation 'collecting 'function)
"Collect things into a list forwards.

Within the body of this macro The form `(COLLECT THING)' will collect
THING into the list returned by COLLECTING.  COLLECT is a local
function so can be passed as an argument,or returned.  COLLECT
returns its argument. See WITH-COLLECTORS for which this COLLECTING is
now a shim"

describe 也可能告诉你有用的东西。通常,像 SLIME 这样的编程环境也会提供各种有用的帮助。我不太了解 SLIME,但在 LW 中,当光标位于 Show Documentation 上时,collecting 命令会弹出一个窗口,显示

Documentation for (defmacro collecting):
Arguments: (&body forms)
<docstring as above>