Lisp 在SICP中开始练习时出错(练习1.3)

Lisp 在SICP中开始练习时出错(练习1.3),lisp,scheme,sicp,Lisp,Scheme,Sicp,提示是定义一个过程,返回三个数中最大的两个数的平方和 我知道这不是一个完美的解决方案,但这就是我拼凑的: (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 (

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

我知道这不是一个完美的解决方案,但这就是我拼凑的:

(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)))))
单词object后面的数字总是正确的答案,顺便说一句。我是一个方案初学者,它一定是我语法中的某个东西


谢谢

正如辛迪卡特所指出的,一个多余的结束括号。很抱歉

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

(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))))
那怎么办

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

您有一个多余的结束括号。使用6作为输入会发生什么情况?:-)为什么要投否决票?我的解决方案是正确的,他已经在他的代码中发现了问题,我只是指出了另一种选择。@ÓscarLópez:或者如果两个值相等且低于另一个值,上述解决方案当然是正确的,但鉴于SICP书中练习1.3的说明,读者不应该“知道”关于
min
程序。只允许使用条件表达式
(define (largest-of-two-sum-of-squares x y z)
    (+ (square x) (square y) (square z)
       (- (square (min x y z)))))