什么时候在lambda表达式前使用#'(函数)? [英] When to use #' (function) in front of lambda expressions?

查看:115
本文介绍了什么时候在lambda表达式前使用#'(函数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,由于Common Lisp中有用于函数和变量的独立命名空间,因此您可以执行以下操作:

 ( (lambda(x)(* 2 x))3)

,您也可以执行以下操作:

 (funcall#'(lambda(x)(* 2 x))3)

什么时候应该使用#'而不是不使用它?我在另一个StackOverflow问题中读到,#’只是出于历史原因而被保留,不应再使用。这是真的?我的问题不是重复的,我是在问何时在代码中使用它们。

解决方案

这不是lisp-2和lisp-1的问题。将 lambda 表达式放在需要函数值的位置,这只是一种风格选择。有些人喜欢#’的视觉标记,有些则不喜欢。 lambda 已经扩展为函数形式,为此#'提供了缩写:


LAMBDA

 (lambda lambda-list [[declaration * | documentation] ]形式*)
==(函数(lambda lambda-list [[declaration * |文档]]形式*))
==#'(lambda lambda-list [[declaration * |文档]]形式*)


#'x 只是(函数x) function 特殊运算符提供名称在当前词法环境中的功能值。 T


特殊运算符 功能



函数的值就是函数的值



如果name是一个函数名,则该名字的函数定义为
,由最内层建立用词法将flet,label或
宏形式(如果有的话)括起来。否则,将返回函数名称的全局函数
定义。


同时(lambda .. 。)是函数的名称,不是 flet label macrolet 形式,因此您总是会得到函数名的全局定义,它只是lambda函数。由于(lambda ...)扩展为(function(lambda ...)),所以没有区别。



但是,请务必注意,在您提到的第一种情况下,

 ((lambda(x)(* x 2))3)

不能这样做:

 (#'(lambda(x) (* x 2))3);或
((函数(lambda(x)(* x 2)))3)

((lambda ...)...)的支持是该语言的一部分,与存在 lambda的定义无关作为宏。这是复合形式的一种特殊类型,即 lambda形式,在HyperSpec中进行了描述:


3.1.2.1 .2.4 Lambda形式



Lambda形式与函数形式相似,不同之处在于函数
名称由lambda表达式代替。 / p>

lambda形式等效于对给定参数使用lambda表达式的
词法闭包的funcall。 (实际上,某些
编译器更有可能以lambda形式
生成内联代码,而不是已声明为内联的任意命名函数;但是
并非语义上的区别。 )



有关更多信息,请参见第3.1.3节(Lambda表达式)



I understand that, because there are separate namespaces in Common Lisp for functions and variables, you can do this:

((lambda (x) (* 2 x)) 3)

and you can also do this:

(funcall #'(lambda (x) (* 2 x)) 3)

When should we use #' as opposed to not using it? I read in another StackOverflow question that #' was only kept around for historic reasons and shouldn't be used anymore. Is this true? My question is not a duplicate, I am asking about when I would use these in my code.

解决方案

It's not an issue of lisp-2 versus lisp-1. With lambda expressions in a position where a function value is needed, it's simply a stylistic choice. Some people like the visual marker of #' and some don't. The lambda macro already expands into the function form for which #' provides an abbreviation:

Macro LAMBDA

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

#'x is just syntactic sugar for (function x), and the function special operator provides "the functional value of name in the current lexical environment." T

Special Operator FUNCTION

The value of function is the functional value of name in the current lexical environment.

If name is a function name, the functional definition of that name is that established by the innermost lexically enclosing flet, labels, or macrolet form, if there is one. Otherwise the global functional definition of the function name is returned.

While (lambda ...) is the name of a function, it's not a name that could ever be established by a flet, label, or macrolet form, so you're always getting "the global definition of the function name", which is just the lambda function. Since (lambda ...) expands to (function (lambda ...)), there's no difference. It's just a matter of style.

However, it is important to note that in the first case that you talked about,

((lambda (x) (* x 2)) 3)

you could not do:

(#'(lambda (x) (* x 2)) 3)            ; or
((function (lambda (x) (* x 2))) 3)

The support for ((lambda ...) ...) is part of the language, unrelated to the fact that there's a definition of lambda as a macro. It's a particular type of compound form, namely a lambda form, which is described in the HyperSpec:

3.1.2.1.2.4 Lambda Forms

A lambda form is similar to a function form, except that the function name is replaced by a lambda expression.

A lambda form is equivalent to using funcall of a lexical closure of the lambda expression on the given arguments. (In practice, some compilers are more likely to produce inline code for a lambda form than for an arbitrary named function that has been declared inline; however, such a difference is not semantic.)

For further information, see Section 3.1.3 (Lambda Expressions).

这篇关于什么时候在lambda表达式前使用#'(函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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