更新时间:2023-02-14 20:02:46
您的代码存在一些问题.
There are some issues in your code.
这将创建一个列表:
(list '(7 5)) ; => ((7 5))
其中的 cdr
始终是一个空列表.
A cdr
of that is always an empty list.
(cdr (list '(7 5))) ; => ()
以这种方式创建单个列表:
A single list is created in this way:
(define l (list 7 5))
或者这样:
(define l '(7 5))
您在 Scheme 中使用括号进行应用.这:
You use parenthesis in Scheme for application. This:
(#t)
表示执行函数#t
".但是 #t
不是函数,它是一个布尔值.并且不能执行布尔值.
means "execute the function #t
". But #t
is not a function, it is a Boolean value. And Boolean values can not be executed.
你可以直接返回一个布尔值
Your can return a Boolean value directly
#t
或者你可以返回一个返回值的函数
or you can return a function returning the value
(lambda () #t)
但是你不能执行true.
but you can not execute true.
或
同样的问题.代码如下:
Same problem in or
. The following code:
(or ((two-subsets (cdr l) (+ s1 1) s2 (+ l1 1) l2)
(two-subsets (cdr l) s1 (+s2 1) l1 (+ l2 1))))
表示:two-subsets
必须返回一个函数.第一个 two-subsets
调用返回的函数与第二个 two-subsets
调用返回的函数一起执行.并且将单个结果值传递给 or
.这可能不是您想要的.
means: two-subsets
must return a function. The function returned by the first two-subsets
call gets executed with the function returned by the second two-subsets
call. And the single result value gets passed to or
. This is probably not what you want.
如果要or
两次调用two-subsets
的两个返回值,必须去掉两个括号.
If you want to or
the two return values of the two calls to two-subsets
, you have to remove two parenthesis.
(or (two-subsets (cdr l) (+ s1 1) s2 (+ l1 1) l2)
(two-subsets (cdr l) s1 (+s2 1) l1 (+ l2 1)))
length
)和总和(您可以使用 apply
将列表传递给 +代码>).剧透
let
.
length
) and sum (you can use apply
to pass a list to +
). Spoilerlet
.