提示是定义一个过程,返回三个数字中最大的两个数字的平方和。
我知道这并不完全是一个优雅的解决方案,但这就是我组合在一起的:
(define (largest-of-two-sum-of-squares x y z)
(cond ((and (< x y) (< x z)) (sum-of-squares y z))
((and (< y z) (< y x)) (sum-of-squares x z))
((and (< z x) (< z y)) (sum-of-squares x y)))))
我想知道为什么我会收到错误。
;The object 85 is not applicable
顺便说一句,单词对象后面的数字始终是正确的答案。我是一个方案初学者,这一定是我语法中的问题?
谢谢
最佳答案
这是另一种可能的解决方案,即使在所有三个数字都相等或两个数字相等且小于另一个的情况下,该解决方案也有效:
(define (sum-max a b c)
(define (sum x y)
(+ (* x x) (* y y)))
(if (>= a b)
(if (>= b c)
(sum a b)
(sum a c))
(if (>= a c)
(sum b a)
(sum b c))))
关于lisp - 在 SICP 中开始练习时出错(练习 1.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9648418/