前几天(也许是昨天),我对#+nil
在https://github.com/billstclair/defperson/blob/master/defperson.lisp#L289中找到的这种读取条件感到非常困惑。
经过一番深思熟虑,我得出的结论是,这是注释代码的非常简单的方法。有人可以确认吗?
也许我的假设是完全错误的。无论如何,预先感谢。
参见CLHS 2.4.8.17 Sharpsign Plus
为了对来自输入的读取表达式进行条件化,Common Lisp使用功能表达式。
在这种情况下,它已用于注释表单。
这是读者的一部分。#+
查看下一个项目(通常是具有相同名称的关键字符号)是否是list的成员*features*
。如果是,则正常读取下一个项目,否则,将其跳过。通常:NIL
不是该列表的成员,因此将跳过该项目。因此,它对Lisp隐藏了表达式。可能存在一个Lisp实现,但无法实现:NIL,新的Lisp实现。它可能:NIL
在*features*
列表上带有符号,以指示实现的名称。
NIL
默认情况下,类似的功能会在keyword
包装中读取:
#+NIL
- >外观为:NIL
在cl:*features*
#+CL:NIL
- >外观为CL:NIL
在cl:*features*
例子
(let ((string1 "#+nil foo bar")) ; string to read from
(print (read-from-string string1)) ; read from the string
(let ((*features* (cons :nil *features*))) ; add :NIL to *features*
(print (read-from-string string1))) ; read from the string
(values)) ; return no values
它打印:
BAR
FOO
请注意,Common Lisp还有其他方法可以注释掉表单:
; (sin 3) should we use that?
#| (sin 3) should we use that?
(cos 3) or this? |#
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句