use*_*349 -1 scheme racket non-procedure-application
我正在用方案编码一个函数,但我收到一个“应用程序:不是过程;期望一个可以应用于参数的过程”错误。我假设我没有正确使用条件语句:
(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))))
'())))))
Run Code Online (Sandbox Code Playgroud)
如果有人能指出我的错误,将不胜感激。
尝试这个:
(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)))
'())))))
Run Code Online (Sandbox Code Playgroud)
学习Scheme时一个很常见的错误:写不必要的括号!请记住:在 Scheme 中,一对()表示function application,所以当你写一些东西 - 像这样的东西时:(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
Run Code Online (Sandbox Code Playgroud)