Common Lisp:#+ nil是什么?

rud*_*ist 5 common-lisp

前几天(也许是昨天),我对https://github.com/billstclair/defperson/blob/master/defperson.lisp#L289中的#+nil读取条件非常困惑.

经过一番深思熟虑后,我得出的结论是,这是评论代码的非常方式.有人能证实吗?

也许我的假设是完全错误的.无论如何,提前谢谢.

ace*_*ent 6

是的,这是注释代码的一种灵活方式,但您不应在生产代码中忽略这一点。

更好的选择是#+(or).

它只需要一个字符,如果你使用 Emacs paredit 或其他一些自动插入右括号的模式,它需要相同的按键,并且它不受符号:nil*features*

  • 不过(至少在我看来)更好的是 `#-(and)`,因为 `#-` 似乎比 `#+` 更像是注释掉某些东西。 (4认同)
  • 如果我感到偏执,我有时会使用`#+#:ignore`。 (3认同)

Rai*_*wig 5

请参阅CLHS 2.4.8.17 Sharpsign Plus

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

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

它是读者的一部分。#+ 查找下一项(通常作为具有相同名称的关键字符号)是否是 list 的成员*features*。如果是,则下一项正常读取,如果不是,则跳过。通常:NIL不是该列表的成员,因此跳过该项目。因此它对 Lisp 隐藏了表达式。可能有一个 Lisp 实现,这不起作用:NILLisp 的新实现。它可能:NIL*features*列表上有符号,以指示实现的名称。

NIL默认情况下,keyword包中读取的功能如下:

  • #+NIL - >外观为:NILcl:*features*
  • #+CL:NIL - >外观为CL:NILcl:*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
Run Code Online (Sandbox Code Playgroud)

它打印:

BAR 
FOO 
Run Code Online (Sandbox Code Playgroud)

请注意,Common Lisp 有其他方式来注释表单:

; (sin 3) should we use that?

#| (sin 3)  should we use that?
   (cos 3)  or this?            |#
Run Code Online (Sandbox Code Playgroud)