首页 > 代码库 > Structure and Interpretation of Computer Programs-Exercise 1.3

Structure and Interpretation of Computer Programs-Exercise 1.3

【问题】

Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

定义一个过程,它以三个数为参数,返回其中较大的两个数的平方和。

【普通版】

(define (sum-square-largest x y z)
  (cond ((and (> y x) (> z x)) ;; y and z are largest
         (+ (* y y) (* z z)))
        ((and (> x y) (> z y)) ;; x and z are largest
         (+ (* x x) (* z z)))
        ((and (> x z) (> y z)) ;; x and y are largest
         (+ (* x x) (* y y)))))

【大神版】

(define (sum-square-largest x y z)
  (cond ((and (< x y) (< x z)) ;; x is smallest
         (+ (* y y) (* z z)))
        (else (sum-square-largest y z x))))

Program is art