2016-10-23 47 views
0
(define (weights# mobile) 
    (cond ((= (is-sculpture? mobile) #t) 1) 
((= (and (= (is-sculpture? mobile) #f) (= (is-sculpture? (right-mobile mobile)) #t) (= (is-sculpture? (left-mobile mobile)) #t)) #t) 3) 
(else (+ 3 (- (weights# (right-mobile mobile)) 1) (- (weights# (left-mobile mobile)) 1)))) 
) 

is-sculpture是返回#t#f的函數。我已設置代碼返回一個數字,具體取決於代碼,但此消息出現在我的屏幕上。方案優先

> contract violation 
    expected: number? 
    given: #f 
    argument position: 1st 
    other arguments...: 

我做了什麼錯了?

回答

0

嗯..根據自己的文字is-sculpture是返回#t#f功能。 =是一個函數,只需要數字參數,因此此調用應該出問題:

(= (is-sculpture? (right-mobile mobile)) #t) ; #t is not a number 

我們檢查一個值與另一個可以使用eq?對於完全相同的對象(#t是同一個對象每次,符號),原始類型爲eqv? .. (eq? 5 5) ; ==> #f,而(eqv? 5 5) ; ==> #tequal?也包括所有基元爲eqv?的相同類型的序列。例如。 (eqv? (list 1 2 3) (list 1 2 3)) ; ==> #f,而(equal? (list 1 2 3) (list 1 2 3)) ; ==> #t

對於返回#t#f的東西,您不需要(eq? (is-sculpture? ...) #t),因爲它與(is-sculpture? ...)相同。要檢查oposite,您只需將結果換算爲not,因此(not (is-sculpture? ...))對於#f結果爲#t

在你的cond的第二項,你知道(is-sculpture? mobile)是不正確的,所以你爲什麼要檢查它是否是錯誤的,當它只能在下一行是錯誤的? cond中的每個術語都可以預期在同一個cond中的所有以前的術語都是錯誤的。

我們實際上並沒有用我們的眼睛解析代碼,所以這個標識告訴我們有多少個parens,而不是真正的parens數量。因此,嚴重格式化的lisp代碼是不可讀的。

在球拍中你需要按CTRL + i它會重新編寫代碼。你非常需要它。還把所有的結束部分放在前一行的末尾。繼承人我怎麼會格式化:

(define (weights# mobile) 
    (cond ((is-sculpture? mobile) 
     1) 
     ; (is-sculpture? mobile) is #f 
     ((and (is-sculpture? (right-mobile mobile)) 
       (is-sculpture? (left-mobile mobile))) 
     3) 
     ; At least one of the other is-sculpture? expressions are false. 
     (else 
     (+ 1 ; 3 + x-1 + y-2 == 1+x+y 
      (weights# (right-mobile mobile)) 
      (weights# (left-mobile mobile)))))) 

除了removnig所有=測試是reduncant我沒有改變的邏輯,我沒有測試的方式,因爲我沒有你的其他功能還是這個哪些參數需要。