使用函数中的 lambda 值作为列表的第一个元素
我正在阅读 Peter Norvig 的《人工智能编程范式》,我遇到了一个我自己无法解决的问题(这是我对 Lisp 的介绍)。这个问题确实很小,但显然不是我的小脑袋能解决的。
为什么当函数的值是 lambda 时,使用该函数作为列表的第一个元素是错误的。例如:
Lisp:
(defun some-func ()
#'(lambda (x) x))
;; At REPL
;; Does not work
> ((some-func) 1)
;; Does work
> ((lambda (x) x) 1)
;; Also works
> (funcall (some-func) 1)
我希望这是有道理的!
I'm reading over Peter Norvig's Paradigms of Artificial Intelligence Programming, and I've come across an issue I cannot resolve on my own (this is my introduction to Lisp). The issue is quite a small one, really, but obviously not one my little brain can solve.
Why is it that when a function's value is a lambda, it is an error to use that function as the first element to a list. For example:
Lisp:
(defun some-func ()
#'(lambda (x) x))
;; At REPL
;; Does not work
> ((some-func) 1)
;; Does work
> ((lambda (x) x) 1)
;; Also works
> (funcall (some-func) 1)
I hope that makes sense!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个很好的问题,但 Common Lisp 可能会非常令人困惑。问题是,由于历史原因,Common Lisp 有两个命名空间——一个用于函数,另一个用于值。为了实现这一点,函数应用程序的头位置和其余部分有两种不同的评估规则——第一个将符号评估为函数名称,第二个将符号评估为变量引用。显然,在某些情况下,该值实际上是一个函数 - 例如,如果您编写一个
mapcar
函数,您会想做类似的事情,但这不起作用 - 它会抱怨
f
是一个未知函数。对于这些情况,有一个名为funcall
的特殊函数,它接收一个函数和该函数的参数,并将照常应用该函数 - 因为funcall
是一个简单的函数函数,它的参数都像往常一样被评估(作为值)。因此,上面的问题应该通过使用它来解决:正如您现在可能怀疑的那样,存在镜像情况 - 您希望将某些内容作为函数进行评估,而不是作为值进行评估。例如,这不起作用:
因为它引用
1+
变量而不是函数。对于这些情况,有一种称为function
的特殊形式,它将其内容作为函数求值并将其作为值返回:并且它可以缩写为
#'
:这不是故事结束 -- 举几个例子:
在某些情况下,一个简单的带引号的名称可以用作函数 -- 例如上一个示例中的
'1+
可以 -- 但是这个是一种只能看到全局绑定名称的 hack,因此#'
几乎总是更好类似hack 可以与
lambda
一起使用 - 因此您可以使用(my-mapcar '(lambda (x) (1+ x)) '(1 2 3))
,事实上,你可以使用(list 'lambda '(x) '(1+ x))
,这甚至更糟糕(和 IIRC,不可移植),但使用(lambda (x ) (1+ x))
可以工作,因为它隐式包装在#'
中(尝试将lambda
形式扩展为宏,您就会看到它) 。一个相关的 hack 可以很好地使用 lambda 表达式作为函数应用程序的头部(这是您尝试过的事情之一)。let
等绑定本地值,在某些情况下,您需要绑定本地函数 - 为此,有新的绑定结构:flet
和 < code>labels如果所有这些看起来很奇怪和/或过于复杂,那么您并不孤单。这是Common Lisp 和Scheme 之间的主要区别之一。 (这种差异导致两种语言中常见习惯用法的变化:Scheme 代码往往比 Common Lisp 代码更频繁地使用高阶函数。像往常一样,处理此类宗教问题时,有些人赞成 CL 的做法,声称高阶函数令人困惑,因此他们喜欢明确的代码内提醒。)
This is a good question, and one where Common Lisp can be pretty confusing. The thing is that due to historical reasons, Common Lisp has two namespaces -- one for functions and another for values. To make this happen, there are two different evaluation rules for the head position of a function application and for the rest -- the first will evaluate a symbol as a function name, and the second will evaluate a symbol as a variable reference. There are obviously cases when the value is actually a function -- for example, if you write a
mapcar
function, you'll want to do something likebut this won't work -- it will complain about
f
being an unknown function. For these cases there is a special function calledfuncall
, which receives a function and argument for the function, and will apply the function as usual -- and sincefuncall
is a plain function, its arguments are all evaluated as usual (as values). So the above should be fixed by using it:As you probably suspect now, there are the mirror cases -- where you want to evaluate something as a function and not as a value. For example, this doesn't work:
because it refers to the
1+
variable and not the function. For these cases there is a special form calledfunction
that evaluates its contents as a function and returns it as a value:and it can be abbreviated with
#'
:That's not the end of this story -- to give a few examples:
in some cases a simple quoted name can work as a function -- for example
'1+
in the last example works -- but this is a kind of a hack that can see only globally bound names, so#'
is almost always bettera similar hack can be used with
lambda
-- so you can use(my-mapcar '(lambda (x) (1+ x)) '(1 2 3))
, in fact, you could use(list 'lambda '(x) '(1+ x))
which is even worse (and IIRC, non-portable), but using(lambda (x) (1+ x))
works since it's implicitly wrapped in a#'
(try expanding alambda
form as a macro and you'll see it). A related hack makes it fine to use alambda
expression as the head of a function application (which is one of the things you've tried).let
etc bind local values, and in some cases you'll want to bind local functions instead -- for this, there are new binding constructs:flet
andlabels
If all of this looks weird and/or overly complicated, then you're not alone. It's one of the main differences between Common Lisp and Scheme. (The difference then leads to changes in common idioms in both languages: Scheme code tends to use higher order functions much more frequently than Common Lisp code. As usual with these kind of religious questions, some people argue in favor of what CL does, claiming that higher order functions are confusing so they like the explicit in-code reminder.)
((lambda (x) ...) ...)
是评估器规则中的硬编码特殊情况。它不会评估表单中的第一个元素并将结果用作一般情况。通常,您必须使用funcall
或apply
来调用作为计算其他形式的结果的函数。((lambda (x) ...) ...)
is a hardcoded special case in the evaluator rules. It's not evaluating the first element in the form and using the result as a general case. It's normal that you have to usefuncall
orapply
to call a function that is the result of evaluating some other form.Common Lisp 不允许返回 lambda 的函数作为表达式中的第一项的原因与 Lisp-1 和 Lisp-2。
如果 Common Lisp 允许
((some-func) 1)
相当于(funcall (some-func) 1)
,那么它可能会被认为是不一致的,不允许说(let ((f #'some-func)) (f 1) )
而不是 require(funcall f 1)
。The reason Common Lisp doesn't allow a function that returns a lambda to be the first item in an expression has to do with the distinction between Lisp-1 and Lisp-2.
If Common Lisp allowed
((some-func) 1)
to be equivalent to(funcall (some-func) 1)
, then it could be perceived as inconsistent to not also allow say(let ((f #'some-func)) (f 1))
rather than require(funcall f 1)
.实际上,不支持此类形式有一个很好的理由:它们含糊不清。考虑一下 Common Lisp 中定义函数名称的方式:
鉴于此,像下面这样的表单应该如何表现?
是否应该将
x
的汽车设置为值10
,或者应该执行表单(setf car)
(这将是一个错误)并尝试使用其返回值作为函数来使用参数10
和x
进行调用? Common Lisp 没有指定这两种行为,而且我根本不清楚这是一个糟糕的选择。毕竟,据我所知,标准中没有任何内容可以阻止一致的实现扩展有效形式的定义以支持更广泛的运算符名称(因此特殊大小写的 setf 函数在这里也没有真正的帮助)。There is actually a very good justification for not supporting such forms: They are ambiguous. Consider the way function names are defined in Common Lisp:
Given this, how should a form like the following behave?
Should this set
x
's car to the value10
, or should it execute the form(setf car)
(which would be an error) and try to use its return value as a function to call with the arguments10
andx
? Common Lisp specifies neither behavior, and it's not at all clear to me that that's a bad choice. After all, as far as I can see, nothing in the standard prevents conforming implementations from extending the definition of valid forms to support a wider range of operator names (so special-casing setf-functions wouldn't really help here, either).