无法将函数调用用作s-exp中的第一个参数 [英] Cannot use function call as first argument in s-exp
问题描述
我正在尝试使用函数以通用Lisp返回函数.但是,我遇到了一种奇怪的情况,想解释一下.
I'm trying to use a function to return a function in common lisp. However, I've run into a strange situation I'd like explained.
这就是我想要做的:
(defun makefun(x) ;1
(lambda (z)
(+ x z)))
((makefun 1) 2) ;2
这将导致非法的函数调用.但是,以下内容是有效的:
This results in an illegal function call. However, the following are valid:
((lambda (z) (+ 1 z)) 2) ;3
(funcall (makefun 1) 2) ;4
为什么不能像第一个示例那样使用makefun?我希望对2中的调用进行评估,以使其等效于第3行.
Why can I not use makefun as in the first example? I'd expect the call in 2 to be evaluated so it would be equivalent to line 3.
推荐答案
如果已在Stackoverflow上回答了类似的问题.这是重复的.需要找到它.
If answered a similar question already on Stackoverflow. It's a duplicate. Would need to find it.
反正.
(defun makefun(x) ;1
(lambda (z)
(+ x z)))
((makefun 1) 2)
Common Lisp不允许在功能位置进行评估.它要求您在此处放置符号或实际的lambda表达式.
Common Lisp does not allow evaluation in the function position. It requires you to put a symbol or an actual lambda expression there.
请记住:Common Lisp有一个单独的 function名称空间. 值命名空间是不同的. MAKEFUN
返回一个值,该值在函数名称空间中不可用.
Remember: Common Lisp has a separate function namespace. The value namespace is different. Here MAKEFUN
returns a value and this value is not available in the function namespace.
只有两种语法调用方法:
There are only two syntactic ways to call a function:
(foo 1 2)
和
((lambda (a b) (list a b)) 1 2)
请参见 CLHS(以表格形式).这里有一个 Lambda表格.
添加编写((some-function-returning-function) 1 2)
的功能会使Common Lisp中的函数调用更加混乱:
Adding the capability to write ((some-function-returning-function) 1 2)
would make function calling in Common Lisp more confusing:
-
(foo 1 2)
将使用函数名称空间 -
((some-function-returning-function) 1 2)
将使用值名称空间
(foo 1 2)
would use the function namespace((some-function-returning-function) 1 2)
would use the value namespace
这篇关于无法将函数调用用作s-exp中的第一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!