为什么我必须调用另一个函数? [英] Why must I funcall a function returned from another?

查看:87
本文介绍了为什么我必须调用另一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不起作用?

( ((lambda () (lambda (x) (funcall #'1+ x)))) 2)
 ; yields Compile-time error: illegal function call

我遇到了这种情况,后来发现 funcall 可以解决它,即

I ran into a situation like this and it later turned out that a funcall fixes it, i.e.

(funcall ((lambda () (lambda (x) (funcall #'1+ x)))) 2) ; => 3

我很困惑,因为似乎第一个应该工作,因为我实际上有一个函数我正在打电话,而不仅仅是可能属于任何名称空间的符号(即(type-of((lambda()#'1 +)))); =>功能)。我认为这有点像您不需要例如 funcall lambda,例如(( λ(x)x):HI); => :HI 。我缺少什么?

I'm confused because it seems like the first one should work, because I actually have a function I'm calling, not just a symbol that may belong to either namespace (i.e. (type-of ((lambda () #'1+))) ; => FUNCTION). I thought it would be kind of like how you don't need to funcall a lambda for example, e.g.((lambda (x) x) :HI) ; => :HI. What am I missing?

推荐答案

Common Lisp的语法要求,每次您想通过以下形式的函数形式调用函数时,类型:

The syntax of Common Lisp requires that, everytime you want to call a function through a compund form of the type:

(f a1 a2 ... an)

列表的第一个元素 f 必须是表示函数名称的符号,或者是表示函数名称的列表 lambda表达式,即(请参见手册):

the first element of the list, f, must be a symbol denoting a function name, or a list denoting a lambda expression, i.e. (see the manual):


lambda表达式 n 。一个列表,它可以在某些情况下代替功能名称,以通过直接描述其行为而不是通过间接引用来表示功能 已建立功能的名称;它的名称来源于它的第一个元素是 symbol lambda。

lambda expression n. a list which 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; its name derives from the fact that its first element is the symbol lambda.

因此,这基本上意味着您不能将返回函数作为值的任何表达式作为第一个元素。在这种情况下,必须使用 funcall

So, this basically means that you cannot have as first element any expression that returns a function as value. In those cases, you must use funcall.

因此,在第二个示例中, funcall (((lambda()(lambda(x)(funcall#'1 + x))))是正确的公称形式,其中列表的第一个元素是lambda表达式(lambda()(lambda(x)(funcall#'1 + x))) (应用于空的参数列表。)

So, in your second example, the first argument of the funcall is ((lambda () (lambda (x) (funcall #'1+ x)))), which is a correct coumpound form, in which the first element of the list is the lambda expression (lambda () (lambda (x) (funcall #'1+ x))) (applied to an empty list of arguments).

在第一个示例中,您将返回函数的表达式作为列表的第一个元素,因此必须使用 funcall

In the first example, instead, you have as first element of the list an expression returning a function, so that you must use funcall.

这篇关于为什么我必须调用另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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