通用Lisp:#+nil是什么?

7
2个回答

6
是的,这是一种lispy风格的代码注释方式,但在生产代码中不应该遗漏它。
更好的替代方法是#+(or)
它只需要多输入一个字符,如果你使用Emacs paredit或其他自动插入闭合括号的模式,它需要相同数目的按键操作,并且不受*features*:nil符号存在的影响。

谢谢提供链接。如果我早点找到它,就不用发帖问问题了,但是#+nil真的很难搜索到。 - rudolfo.christ
4
在我看来,#-更好,因为它似乎更像是注释掉某些东西,而不是#+ - Joshua Taylor
3
有时候我会用 #+#:ignore,如果我感到很神经紧张的话。 - Luís Oliveira

5

请查看 CLHS 2.4.8.17 Sharpsign Plus

为了有条件地从输入中读取表达式,Common Lisp 使用特性 expressions

在这种情况下,它被用于注释掉一个表单。

它是阅读器的一部分。#+ 查看下一个项,通常是具有相同名称的关键字符号,是否是列表 *features* 的成员。如果是,则下一个项按正常方式读取,如果不是,则跳过该项。通常,:NIL 不是该列表的成员,因此该项被跳过。因此它将表达式隐藏在Lisp中。可能会有一个Lisp实现,其中这不起作用:NILNew Implementation of Lisp。它可能已经在*features*列表上有符号:NIL,以指示实现的名称。

NIL 这样的特性默认在 keyword 包中读取:

  • #+NIL -> 在 cl:*features* 中查找 :NIL
  • #+CL:NIL -> 在 cl:*features* 中查找 CL:NIL

示例

(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?            |#

作为一条注释:看起来 SBCL 忽略了 #+nil ... 表达式,即使 NIL *features* 中。如果写入文件,然后加载,以下内容将打印“Nope”:(push nil *features*) #+nil (format t "NIL is in *features*") #-nil (format t "Nope") - Inaimathi
1
@Inaimathi,这是因为特性表达式被读入关键字包中。在*features*中,:NIL将是相关的符号。 - m-n
@m-n:是的。将上面的第一个表达式替换为(push :nil *features*) ...,会打印出NIL is in *features* - Inaimathi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接