Emacs Lisp:(function (lambda ...)) 和 (lambda ...) 之间的区别? [英] Emacs Lisp: difference between (function (lambda ...)) and (lambda ...)?

查看:19
本文介绍了Emacs Lisp:(function (lambda ...)) 和 (lambda ...) 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别

(function (lambda ...))

(lambda ...) 

'(lambda ...)

?

在很多情况下,这三个似乎可以互换.

It seems three are interchangeable in a lot of cases.

推荐答案

它们可以互换.答案是 function 使 lambda 能够被字节编译,而其他两个则没有(并且是等效的).注意:这并不意味着 function 实际上对 lambda 进行字节编译.

They are pretty interchangeable. The answer is that function enables the lambda to be byte compiled, whereas the other two do not (and are equivalent). Note: this does not mean that function actually byte compile the lambda.

人们怎么会想到这一点?一点 Emacs lisp 内省提供了一些线索.开始:C-h f 函数 RET:

How might one figure that out? A little Emacs lisp introspection provides some clues. To start: C-h f function RET:

函数是'C中的一种特殊形式源代码'.

function is a special form in 'C source code'.

(函数参数)

喜欢'quote',但首选作为函数的对象.以字节为单位编译,函数"导致其要编译的参数.'引用'不能那样做.

Like 'quote', but preferred for objects which are functions. In byte compilation, 'function' causes its argument to be compiled. 'quote' cannot do that.

好的,这就是 (function (lambda ...))'(lambda ...) 之间的区别,第一个告诉字节编译器它可以安全地编译表达式.而 ' ed 表达式可能不一定被编译(因为它们可能只是一个数字列表.

Ok, so that's the difference between (function (lambda ...)) and '(lambda ...), the first tells the byte compiler that it may safely compile the expression. Whereas the 'ed expressions may not necessarily be compiled (for they might just be a list of numbers.

仅仅使用简单的 (lambda ...) 怎么样?C-h f lambda RET 显示:

What about just the bare (lambda ...)? C-h f lambda RET shows:

lambda 是 `subr.el' 中的 Lisp 宏.

lambda is a Lisp macro in `subr.el'.

(lambda args [docstring] [interactive]体)

(lambda args [docstring] [interactive] body)

返回一个 lambda 表达式.一个电话表单(lambda args 文档字符串互动体)是自引;这评估 lambda 的结果表达式是表达式本身.然后 lambda 表达式可能是作为函数处理,即存储为符号的函数值,传递'funcall' 或 'mapcar' 等

Return a lambda expression. A call of the form (lambda args docstring interactive body) is self-quoting; the result of evaluating the lambda expression is the expression itself. The lambda expression may then be treated as a function, i.e., stored as the function value of a symbol, passed to 'funcall' or 'mapcar', etc.

因此,(lambda ...)'(lambda ...) 是等价的.

Therefore, (lambda ...) and '(lambda ...) are equivalent.

还有符号#'(lambda ...),它是(function (lambda ...))的语法糖.

Also, there is the notation #'(lambda ...), which is syntactic sugar for (function (lambda ...)).

有关 Emacs lisp 中函数的更多信息,请阅读 Functions信息页面.

For more information on functions in Emacs lisp, read the Functions info pages.

为了检查所有这些,您可以在 *scratch* 缓冲区中键入以下内容并计算表达式:

Just to check all this, you can type the following into the *scratch* buffer and evaluate the expressions:

(caddr '(lambda (x) (+ x x)))
(+ x x)

(caddr (lambda (x) (+ x x)))
(+ x x)

(caddr (function (lambda (x) (+ x x))))
(+ x x)

(equal '(lambda (x) (+ x x))
       (function (lambda (x) (+ x x))))
t

(equal '(lambda (x) (+ x x))
       (lambda (x) (+ x x)))
t

因此,使用 lambda 的所有三种变体都只是构建了可用作函数的列表(其中之一可能是字节编译的).

So, all three variants of using lambda just build up lists that may be used as functions (one of which may be byte compiled).

这篇关于Emacs Lisp:(function (lambda ...)) 和 (lambda ...) 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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