2

提示是定义一个过程,该过程返回三个数字中最大的两个的平方和。

我知道这不是一个优雅的解决方案,但这是我一起破解的:

(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 后面的数字始终是正确答案。我是一个方案初学者,它一定是我的语法吗?

谢谢

4

3 回答 3

3

这是另一种可能的解决方案,即使在所有三个数字都相等或两个数字相等且小于另一个的情况下,这个解决方案也有效:

(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-10T17:36:25.627 回答
1

关于什么

(define (largest-of-two-sum-of-squares x y z)
    (+ (square x) (square y) (square z)
       (- (square (min x y z)))))

?

于 2012-03-11T22:17:06.937 回答
1

正如 sindikat 指出的那样,一个多余的右括号。对于那个很抱歉。

于 2012-03-10T17:18:55.083 回答