2014-10-29 35 views
-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)))) 
       '()))))) 

如果任何人都可以指出我的錯誤,將不勝感激。

+0

[應用不是一個程序(流程地圖程序)](http://stackoverflow.com/questions/21855124/application-not-a-procedure-scheme-map-procedure) – uselpa 2014-10-29 15:24:11

+0

的可能重複在二進制算術過程中[「應用程序:不是過程」)的可能重複(http://stackoverflow.com/questions/19022704/application-not-a-procedure-in-binary-arithmetic-procedures) – 2014-10-29 16:02:02

+2

這不顯示了很多研究努力; Google搜索['site:stackoverflow.com「application:not a procedure」'](https://www.google.com/search?q=site%3Astackoverflow.com+%22Scheme+-+application%3A+not+ a + procedure + error%22)在堆棧溢出中出現了很多*結果,它們都是關於錯位的括號。搜索確切的錯誤消息是一個很好的練習。另外,Racket的編輯不會突出顯示問題出在哪裏,正如我鏈接到的副本所示。 – 2014-10-29 16:04:04

回答

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

這是學習方案時一個非常常見的錯誤:寫不必要的括號!請記住:在一對()意味着函數應用,所以當你寫東西 - 如下所示:(f),計劃試圖應用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 
+0

簡單的刪除括號,非常讚賞。 – user3352349 2014-10-29 14:51:09

+0

@ user3352349是的:)但重要的是你明白_why_他們錯了! – 2014-10-29 14:59:09