SICP之1.3中文翻譯題目錯誤
原版題目:
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 (square x) (* x x))
(define (sumsquares x y) (+ (square x) (square y)))
(define (sqsumlargest a b c)
(cond
((and (>= a c) (>= b c)) (sumsquares a b))
((and (>= b a) (>= c a)) (sumsquares b c))
((and (>= a b) (>= c b)) (sumsquares a c))))
我個人的答案是:
(define (sum-max-three x y z)
(define (bigger a b) (if (< a b)
b
a))
(define (smaller a b)
(if (= (bigger a b) b)
a
b))
(define (square x)
(* x x))
(+ (square (bigger x y)) (square (bigger (smaller x y) z) )))
思路是:兩個數比較,取兩個數中最大一個平方 加上 前面兩個數中較小的如第三個數比較取較大的平方。
列如 1 2 3 max(1,2)-> 2 max(1,3)-> 3 2平方 + 3平方。