lisp - should be a lambda expression

拟墨画扇 提交于 2019-12-12 10:23:54

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/22163216/lisp-should-be-a-lambda-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!