【发布时间】:2016-01-30 13:57:15
【问题描述】:
我正在学习 Common Lisp,想玩 lisp 和 Web 开发。我当前的问题来自一个简单的想法,即遍历我想要包含的所有 javascript 文件。我使用 SBCL 和 Quicklisp 来快速启动。问题可能与我正在使用的cl-who 包有关。
所以我已经声明了我的包并开始这样:
(defpackage :0xcb0
(:use :cl :cl-who :hunchentoot :parenscript))
(in-package :0xcb0)
为了简单起见,我减少了问题函数。所以我有这个page 函数:
(defun page (test)
(with-html-output-to-string
(*standard-output* nil :prologue nil :indent t)
(:script
(:script :type "text/javascript" :href test))))
这将产生所需的输出
*(0xcb0::page "foo")
<script>
<script type='text/javascript' href='foo'></script>
</script>
现在我创建了一个生成:script 标签的宏。
(defmacro js-source-file (filename)
`(:script :type "text/javascript" :href ,filename)))
这按预期工作:
*(macroexpand-1 '(0XCB0::js-source-file "foo"))
(:SCRIPT :TYPE "text/javascript" :HREF "foo")
但是,如果我将其包含在我的 page 函数中:
(defun page (test)
(with-html-output-to-string
(*standard-output* nil :prologue nil :indent t)
(:script
(js-source-file "foo"))))
...在定义新的page 函数时,它会给我一个样式警告(undefined function: :SCRIPT)。此外,page 函数在执行时会产生此错误:
*(0xcb0::page "foo")
The function :SCRIPT is undefined.
[Condition of type UNDEFINED-FUNCTION]
为什么嵌入的宏 js-source-file 会按预期工作,因为它会产生所需的输出,但在另一个函数中调用时却失败了?
附:我知道对于像我这样的初学者来说,Lisp 中的宏主题可能会让人筋疲力尽。但目前我无法理解这应该有效但无效的事实!
【问题讨论】:
-
稍后我将不得不对其进行测试,但问题可能在于
with-html-output-to-string是一个宏,它将其中的(:script ...)表单转换为其他代码。由于js-source-file还没有展开,所以with-html-output-to-string不处理。
标签: macros common-lisp cl-who