【问题标题】:Scheme - application: not a procedure error方案 - 应用程序:不是程序错误
【发布时间】:2020-11-01 21:46:32
【问题描述】:

我正在编写一个方案中的函数,但我得到一个“应用程序:不是过程; 期望可以应用于参数的过程”错误。我假设我没有正确使用条件语句:

(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 racket non-procedure-application


【解决方案1】:

试试这个:

(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

【讨论】:

  • 删除括号的简单问题,仍然非常感谢。
  • @user3352349 是的 :) 但重要的是您要了解为什么它们放错了位置!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多