违约
Contract Violation
我正在尝试编写一个递归函数来测试输入的整数中的数字是否按升序排列。当我 运行 我的代码出现违反合同的错误时。
(define (rising-numbers n)
(if(zero? (truncate n))
(modulo n 10)
(> (modulo n 10) (rising-numbers (quotient n 10)))))
(rising-numbers 123)
这是我遇到的错误:
>: contract violation
expected: real?
given: #t
argument position: 2nd
other arguments...:
这道题对于步进器来说完美。确保语言级别为 "Beginning Student",只输入您在定义 window 中粘贴的代码,然后单击 "step"。我想您很快就会发现问题!
看看你的函数我发现你函数 returns a number when (zero? (truncate n))
and a boolean 如果它是假的。原因是 >
总是 return 布尔值 #t
或 #f
。虽然 returning 不同的类型是语言的一个特性,但当它在运行时变得不可预测时通常是一个错误。
根据错误消息,您已将布尔值作为 >
的参数之一。所以问题出在你唯一使用 >
:
的地方
(> (modulo n 10) (rising-numbers (quotient n 10)))
在这里你做 (rising-numbers (quotient n 10))
作为一个数字参数但是因为我们已经在第一部分建立了这可以 return 一个布尔值你不能使用这个值作为 >
中的第二个参数.
那么你是怎么做到的呢。出色地。一如既往,我们有基本情况:
(rising-numbers 2); ==> #t
现在默认情况下应该这样做:
(rising-numbers 123) ; ==
(and (<= 2 3) (rising-numbers 12)) ; ==
(and (<= 2 3) (and (<= 1 2) (rising-numbers 1))) ; ==
(and #t #t) ; ==> #t
在这种情况下,rising-numbers
永远不会 return 数字,始终是布尔值,并且您需要在每个步骤中检查当前值和下一个值。事实上,你得到一个基本情况和当前和下一个数字之间的 n-1 比较。
我正在尝试编写一个递归函数来测试输入的整数中的数字是否按升序排列。当我 运行 我的代码出现违反合同的错误时。
(define (rising-numbers n)
(if(zero? (truncate n))
(modulo n 10)
(> (modulo n 10) (rising-numbers (quotient n 10)))))
(rising-numbers 123)
这是我遇到的错误:
>: contract violation
expected: real?
given: #t
argument position: 2nd
other arguments...:
这道题对于步进器来说完美。确保语言级别为 "Beginning Student",只输入您在定义 window 中粘贴的代码,然后单击 "step"。我想您很快就会发现问题!
看看你的函数我发现你函数 returns a number when (zero? (truncate n))
and a boolean 如果它是假的。原因是 >
总是 return 布尔值 #t
或 #f
。虽然 returning 不同的类型是语言的一个特性,但当它在运行时变得不可预测时通常是一个错误。
根据错误消息,您已将布尔值作为 >
的参数之一。所以问题出在你唯一使用 >
:
(> (modulo n 10) (rising-numbers (quotient n 10)))
在这里你做 (rising-numbers (quotient n 10))
作为一个数字参数但是因为我们已经在第一部分建立了这可以 return 一个布尔值你不能使用这个值作为 >
中的第二个参数.
那么你是怎么做到的呢。出色地。一如既往,我们有基本情况:
(rising-numbers 2); ==> #t
现在默认情况下应该这样做:
(rising-numbers 123) ; ==
(and (<= 2 3) (rising-numbers 12)) ; ==
(and (<= 2 3) (and (<= 1 2) (rising-numbers 1))) ; ==
(and #t #t) ; ==> #t
在这种情况下,rising-numbers
永远不会 return 数字,始终是布尔值,并且您需要在每个步骤中检查当前值和下一个值。事实上,你得到一个基本情况和当前和下一个数字之间的 n-1 比较。