在 reader 宏中使用时读取的递归 p 参数
read's recursive-p argument when used inside a reader macro
我写了 Scheme 的 s 表达式注释的 Common Lisp 实现 (SRFI 62):
(eval-when (:compile-toplevel
:load-toplevel
:execute)
(set-dispatch-macro-character #\# #\;
(lambda (stream char n)
(declare (ignore char))
(when n
(error "Infix parameter not allowed in s-expression comment."))
(read stream) ; Discard the s-expression.
(values))))
根据我对 The RECURSIVE-P argument 的阅读,我的实现似乎不正确。我必须改用 (read stream t nil t)
(即将 recursive-p
参数设置为 t
)。我的理解正确吗?
我一直在使用上面明显不正确的实现,它似乎可以正常运行。如果我继续使用 (read stream)
而不是 (read stream t nil t)
会发生什么?
举一个不正确行为的例子,让我们考虑 RECURSIVE-P argument documentation 中给出的三个原因中的第一个。首先,使用您的原始定义:
* (+ 1 #1=2 #;(+ #1# 3))
Reader error: Missing #1# label.
将 (read stream)
更改为 (read string t nil t)
给出正确的结果:
* (+ 1 #1=2 #;(+ #1# 3))
3
我写了 Scheme 的 s 表达式注释的 Common Lisp 实现 (SRFI 62):
(eval-when (:compile-toplevel
:load-toplevel
:execute)
(set-dispatch-macro-character #\# #\;
(lambda (stream char n)
(declare (ignore char))
(when n
(error "Infix parameter not allowed in s-expression comment."))
(read stream) ; Discard the s-expression.
(values))))
根据我对 The RECURSIVE-P argument 的阅读,我的实现似乎不正确。我必须改用 (read stream t nil t)
(即将 recursive-p
参数设置为 t
)。我的理解正确吗?
我一直在使用上面明显不正确的实现,它似乎可以正常运行。如果我继续使用 (read stream)
而不是 (read stream t nil t)
会发生什么?
举一个不正确行为的例子,让我们考虑 RECURSIVE-P argument documentation 中给出的三个原因中的第一个。首先,使用您的原始定义:
* (+ 1 #1=2 #;(+ #1# 3))
Reader error: Missing #1# label.
将 (read stream)
更改为 (read string t nil t)
给出正确的结果:
* (+ 1 #1=2 #;(+ #1# 3))
3