問題 1.3

無職の間にSICPも読んでみる。目標は焦らずしっかりと読む。問題の解答をさらす。解く順番は出来るだけシーケンシャル。

問題 1.3
三つの数を引数としてとり、大きい二つの数の二乗の和を返す手続きを定義せよ。


Exercuse 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-of-squares-two-of-three a b c)
  (if (and (> a c) (> b c)) (+ (* a a) (* b b))
      (if (and (> a b) (> c b)) (+ (* a a) (* c c))
      (+ (* b b) (* c c)))))


; いずれも 3 と 4 の二乗の和 25 となる
(sum-of-squares-two-of-three 2 3 4)
(sum-of-squares-two-of-three 2 4 3)
(sum-of-squares-two-of-three 3 2 4)
(sum-of-squares-two-of-three 3 4 2)
(sum-of-squares-two-of-three 4 2 3)
(sum-of-squares-two-of-three 4 3 2)