方案-应用程序:不是过程错误



我正在scheme中编码一个函数,但我得到了一个"应用程序:而不是过程;期望一个可以应用于arguments错误的过程。我认为我没有正确使用条件语句:

(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))))
              '())))))

如果有人能指出我的错误,我将不胜感激。

试试这个:

(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