提示是定义一个过程,该过程返回三个数字中最大的两个数字的平方和。
我知道这并不是一个优雅的解决方案,但这是我总结出来的:
(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单词object后面的数字始终是正确答案,btw。我是一个方案初学者,它必须在我的语法中的一些东西?
谢谢
发布于 2012-03-11 01:36:26
这是另一种可能的解决方案,即使在三个数字都相等或两个数字相等且小于另一个的情况下也可以使用:
(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))))发布于 2012-03-11 01:18:55
正如sindikat指出的那样,过多的结束括号。真对不起。
发布于 2012-03-12 06:17:07
关于
(define (largest-of-two-sum-of-squares x y z)
(+ (square x) (square y) (square z)
(- (square (min x y z)))))https://stackoverflow.com/questions/9648418
复制相似问题