我正在编写一个方案中的函数,但我得到的是一个“应用程序:不是一个程序; 期望一个可以应用于参数“错误的过程。我假设我没有正确使用条件语句:
(define find-allocations
(lambda (n l)
(if (null? l)
'()
(cons ((if (<=(get-property (car l) 'capacity) n)
(cons (car l) (find-allocations (- n (get-property (car l) 'capacity)) (cdr l)))
'()))
(if (<=(get-property (car l) 'capacity) n)
(cons (car l) (find-allocations (n (cdr l))))
'())))))
如果有人能够指出我的错误,那将非常感激。
答案 0 :(得分:4)
试试这个:
(define find-allocations
(lambda (n l)
(if (null? l)
'()
(cons (if (<= (get-property (car l) 'capacity) n)
(cons (car l) (find-allocations (- n (get-property (car l) 'capacity)) (cdr l)))
'())
(if (<= (get-property (car l) 'capacity) n)
(cons (car l) (find-allocations n (cdr l)))
'())))))
学习Scheme时,这是一个非常常见的错误:编写不必要的括号!请记住:在Scheme中,一对()
表示函数应用程序,因此当您编写某些内容时 - (f)
,Scheme尝试应用f
,就好像这是一个程序,在你的代码中你有几个地方发生这种情况:
((if (<=(get-property (car l) 'capacity) n) ; see the extra, wrong ( at the beginning
(find-allocations (n (cdr l)))) ; n is not a function, that ( is also mistaken