为什么在CLISP中不需要使用#'(尖括号)符号? [英] Why is #' (sharp-quote) notation unnecessary in CLISP?

查看:90
本文介绍了为什么在CLISP中不需要使用#'(尖括号)符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从"Practical Common Lisp"一书中学习Lisp.在某一点上,我应该输入以下代码:

I'm learning Lisp from the book 'Practical Common Lisp'. At one point, I'm supposed to enter the following bit of code:

[1] (remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10))
(2 4 6 8 10)

我想这里的想法当然是remove-if-not想要一个可以在提供参数时返回T或NIL的函数,然后将该函数应用于列表中的所有符号,并返回列表仅包含返回NIL的那些符号.

I suppose the idea here is of course that remove-if-not wants a function that can return either T or NIL when an argument is provided to it, and this function is then applied to all symbols in the list, returning a list containing only those symbols where it returned NIL.

但是,如果我现在在CLISP中编写以下代码:

However, if I now write the following code in CLISP:

[2] (remove-if-not 'evenp '(1 2 3 4 5 6 7 8 9 10)
(2 4 6 8 10)

它仍然有效!所以我的问题是,无论是使用尖引号表示法还是仅使用引号就足够了吗?现在看来,额外的尖锐只是让程序员知道嘿,这是一个函数,而不仅仅是一些随机符号!" -但是,如果还有其他用途,我很想知道.

It still works! So my question is, does it even matter whether I use sharp-quote notation, or is just using the quote sufficient? It now seems like the additional sharp is only there to let the programmer know that "Hey, this is a function, not just some random symbol!" - but if it has any other use, I'd love to know about it.

我使用的是GNU CLISP 2.49(2010年7月7日,实际算是相当老了).

I use GNU CLISP 2.49 (2010-07-07, sheesh that's actually pretty old).

推荐答案

在通常情况下,右引号和quote不会具有相同的行为:

Sharp-quote and quote do not have the same behaviour in the general case:

(defun test () 'red)

(flet ((test () 'green))
  (list (funcall 'test)
        (funcall #'test))) => (red green)

调用带引号的符号将使用带引号的符号的函数值(即,symbol-function的结果).调用带引号的符号将使用该符号的词法绑定(如果有)所建立的值.在公认的没有词法绑定的常见情况下,行为将是相同的.这就是您所看到的.

Calling a quoted symbol will use the function value of the quoted symbol (ie, the result of symbol-function). Calling a sharp-quoted symbol will use the value established by the lexical binding, if any, of the symbol. In the admittedly common case that there is no lexical binding the behaviour will be the same. That's what you are seeing.

您应该养成使用尖引号的习惯.忽略函数绑定可能不是您想要的,并且可能使任何试图理解您的代码的人感到困惑.

You should get into the habit of using sharp-quote. Ignoring function bindings is probably not what you want, and may be confusing to anybody trying to understand your code.

这篇关于为什么在CLISP中不需要使用#'(尖括号)符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆