私はコンピュータープログラムの構造と解釈に従っています.Ex 1.3を解決しようとしているときに、最初の試みとして次のコードにたどり着きました:
(define (sumsq a b c)(
(define highest (if (> (if (> a b) a b) (if (> a c) a c)) (if (> a b) a b) (if (> a c) a c)))
(define second_h (if (> (if (> a b) b a) (if (> a c) c a)) (if (> a b) b a) (if (> a c) c a)))
(+ (* highest highest) (* second_h second_h)))
うまくいかなかったので、解決策を調べてSICP Wikiで見つけました
;; ex 1.3
;; implemented using only techniques covered to this point
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (largest-two-of-three x y z)
(if (>= x y)
(sum-of-squares x (if (>= y z) y z))
(sum-of-squares y (if (>= x z) x z))))
違いは、複数のステートメントを使用して変数を定義し、2 乗を合計していたことですが、正しい方法は各行を関数として定義することです。
スキームの関数はワンライナーですか? それとも私はすべてを逃したのですか?