为什么在Common Lisp中的lambda之前使用#'? [英] Why #' is used before lambda in Common Lisp?

查看:73
本文介绍了为什么在Common Lisp中的lambda之前使用#'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我看到的大多数Common Lisp代码都有类似的东西

I would like to know why most Common Lisp code I see has things like

(mapcar#'(lambda(x)(* xx))'(1 2 3))

而不是

(mapcar(lambda(x)(* xx))'(1 2 3))

以及工作。我开始学习Common Lisp,并且对Scheme有一些背景,这引起了我的兴趣。

which seems to work as well. I am beginning to learn Common Lisp, and having some background in Scheme, this intrigues me.

编辑:我知道您需要#'具有函数名,因为它们与变量位于不同的命名空间中。我的问题是在lambda之前#号,因为lambda已经返回了一个函数对象(我认为)。少#'的lambda由于宏扩展而起作用的事实使其变得更加有趣...

I know that you need #' with function names because they live in a different namespace than variables. My question is just about #' before lambda, as lambda already returns a function object (I think). The fact that #'-less lambdas work because of a macro expansion just makes it more intriguing...

推荐答案

#'foo 缩写供读者(函数foo)使用。

在CL中,有几个不同的命名空间,#'foo (function foo)将返回<$ c $的功能值 c> foo 。

In CL, there are several different namespaces, #'foo or (function foo) will return the functional value of foo.

您可能想搜索 Lisp-1 vs. Lisp-2 ,检查其他 Stackoverflow问题,或阅读旧文章,以便更多地了解多个名称空间(也称为符号的插槽单元格)。

You may want to search for "Lisp-1 vs. Lisp-2", check other Stackoverflow questions, or read an old article by Pitman and Gabriel in order to learn more about the concept of multiple namespaces (also called slots or cells of symbols).

对于lambda的原因,在CL中可以省略#',因为它是一个宏,因此可以扩展(取自超规格):

The reason that, in the case of lambda, the #' may be omitted in CL is that it is a macro, which expands thusly (taken from the Hyperspec):

(lambda lambda-list [[declaration* | documentation]] form*)
==  (function (lambda lambda-list [[declaration* | documentation]] form*))
==  #'(lambda lambda-list [[declaration* | documentation]] form*)

#'仍然出于历史原因而使用(我认为在Maclisp lambda 中没有扩展为函数形式),或者因为某些人认为,使用尖引号标记lambda可能会使代码更具可读性或连贯性。在某些特殊情况下,这可能会有所作为,但总的来说,选择哪种形式并不重要。

#' may still be used for historic reasons (I think that in Maclisp lambdas didn't expand to the function form), or because some people think, that tagging lambdas with sharpquotes may make the code more readable or coherent. There may be some special cases in which this makes a difference, but in general, it doesn't really matter which form you choose.

我想您可以想到像这样:(function(lambda ...))返回创建的函数(lambda ...)。请注意,CL Hyperspec中的 lambda 都具有宏和符号条目。从后者开始:

I guess you can think of it like this: (function (lambda ...)) returns the function (lambda ...) creates. Note that lambda in the CL Hyperspec has both a macro AND a symbol entry. From the latter:


lambda表达式是一个列表,可以使用
代替$ b $中的函数名。 b在某些情况下通过直接描述函数
的行为来表示函数
,而不是通过引用
来建立函数的名称来间接表示函数。

A lambda expression is a list that can be used in place of a function name in certain contexts to denote a function by directly describing its behavior rather than indirectly by referring to the name of an established function.

来自文档 >功能:

From the documentation of function:


如果name是lambda表达式,则返回
词法闭包。

If name is a lambda expression, then a lexical closure is returned.

我认为区别还与调用lambda形式有关:((lambda ...) ...)视为要评估的形式,而(funcall#'(lambda ...)...)。如果您想了解更多有关该主题的信息,请参阅 cll线程

I think the difference is also related to calling lambda forms like this: ((lambda ...) ...) where it is treated as a form to be evaluated, vs. (funcall #'(lambda ...) ...). If you want to read more on the topic, there is a c.l.l thread about it.

该线程的一些引号:


(lambda(x)... 本身只是一些
不带引号的列表结构,它是
作为参数的外观到
函数特殊形式(函数
(lambda(x)...
导致
函数对象存在

(lambda (x) ... by itself is just some unquoted list structure. It is its appearance as an argument to the FUNCTION special form (function (lambda (x) ... that causes the function object to exist

和:


事实也很复杂LAMBDA宏的
是ANSI Common Lisp的一个较晚的
附加功能,因此所有
的真正老家伙(例如,像我一样)
都在需要时学习了他们的lisp
在映射函数中为lambda表达式
提供#',否则
不存在的lambda函数将$

It's also compounded by the fact that the LAMBDA macro was a rather late addition the ANSI Common Lisp, so all of the really old guys (i.e., like me) learned their lisp when you needed to supply the #' to the lambda expression in the mapping functions. Otherwise the non-existent lambda function would be invoked.

宏的添加改变了这一点,但是
我们中有些人对我们想要改变的方式太过偏执。

The macro addition changed that, but some of us are too set in our ways to want to change.

这篇关于为什么在Common Lisp中的lambda之前使用#'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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