lisp-应该是lambda表达式 [英] lisp - should be a lambda expression
问题描述
我正在尝试从其所驻留的函数返回(值str((+ x 3)y)).
代码段:
(if (<my condition>)
(values str ((+ x 3) y))
(values str ((+ x 2) y)))
给出错误:
(+ X 3) SHOULD BE A LAMBDA EXPRESSION
但是(values str (y (+ x 3)))
可以正常工作.
为什么?
S表达式((+ x 3) y)
不能求值,因为第一个列表元素不是可调用的(它应该命名一个函数或为lambda表达式).>
因此,为了避免评估,您需要引用它:
(if (<my condition>)
(values str '((+ x 3) y))
(values str '((+ x 2) y)))
然后,您将返回长度为2的列表(包含长度为3的列表和符号y
)作为第二个值.但是,如果要返回列表中(+ x 2)
和y
的值,则需要执行类似
(values str (list (+ x (if <condition> 3 2)) y))
或者返回3个值,而不是2个:
(values str
(+ x (if <condition> 3 2))
y)
另一方面,y
是一个符号,显然,它在图像中命名了一个函数,因此(y (+ x 3))
的求值效果很好(它将3
添加到
I'm trying to return (values str ((+ x 3) y)) from the function it resides in.
code snippet:
(if (<my condition>)
(values str ((+ x 3) y))
(values str ((+ x 2) y)))
gives error:
(+ X 3) SHOULD BE A LAMBDA EXPRESSION
but (values str (y (+ x 3)))
works fine.
why?
The S-expression ((+ x 3) y)
cannot be evaluated because the first list element is not funcallable (it should name a function or be a lambda expression).
So, to avoid evaluation, you need to quote it:
(if (<my condition>)
(values str '((+ x 3) y))
(values str '((+ x 2) y)))
Then you will return a list of length 2 (containing a list of length 3 and a symbol y
) as your second value. If, however, you want to return the values of (+ x 2)
and y
in the list, you will want to do something like
(values str (list (+ x (if <condition> 3 2)) y))
or maybe return 3 values instead of 2:
(values str
(+ x (if <condition> 3 2))
y)
On the other hand, y
is a symbol, which, apparently, names a function in your image, so (y (+ x 3))
evaluates fine (it calls function y
on the result of adding 3
to x
).
这篇关于lisp-应该是lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!