为什么将#'与lambda一起使用? [英] Why use #' with lambda?
问题描述
为什么我应该在Lambda中使用#'?通常以这种方式编写,因此我认为它是一种很好的形式.但是这些行似乎和我一样:
Why should I use #' together with lambda? It is usually written that way, so I guess it is good form. But these lines seem equal to me:
> (mapcar #'(lambda (x) (+ x 1)) '(1 2 3))
(2 3 4)
> (mapcar (lambda (x) (+ x 1)) '(1 2 3))
(2 3 4)
有人介意让初学者了解两者之间的区别吗?
Anyone care to enlighten a beginner about the difference?
推荐答案
#'
是function
的简写,它返回指向函数的指针(而不是应用它). lambda
返回一个函数,通常需要一个指向该函数的指针.由于这很常见,因此还有一个宏(在可变空间中)为您执行此操作,该宏也称为lambda
.这两行代码是相同的.
#'
is shorthand for function
, which returns a pointer to a function (instead of applying it). lambda
returns a function, and it's usual to want a pointer to that function. Since this is so common, there is also a macro (in variable space) that does it for you, which is called lambda
as well. Both lines of code are identical.
哪个更好,取决于 Lisp-1/Lisp-2辩论:在宏Lisp中,您可以在Common Lisp中执行任一操作.一如既往,保持一致.
Which is better comes down to the Lisp-1/Lisp-2 debate: in Common Lisp you can do either, thanks to the macro. As always, be consistent.
这篇关于为什么将#'与lambda一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!