2016-10-23 45 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我没有改变的逻辑,我没有测试的方式,因为我没有你的其他功能还是这个哪些参数需要。