***** 練習1の解答例 *****
(define apple 110)
(define orange 90)
(define pineapple 230)
(define peech 190)
(define melon 800)
(define box 50)
(define giftbox (+ (* melon 1) (* apple 5) (* orange 3) box))
(define specialbox (+ (* (* peech 0.9) 3) pineapple (* (* orange 0.8) 5)))
***** 練習2の解答例 *****
(define f (lambda (x)
(+ (* x x) (* 5 x) 6)))
(define g (lambda (x)
(* (+ x 2) (+ x 3))))
(define d (lambda (a b c)
(- (* b b) (* 4 a c))))
(define heron4 (lambda (a b c s)
(sqrt (* s (- s a) (- s b) (- s c)))))
(define heron (lambda (a b c)
(heron4 a b c (/ (+ a b c) 2))))
|
上記は、ヒント1の解答例である。heron が heron4 を呼び出すインタフェースになっている。このような書き方は、後で学ぶ Prolog ではよく使われる。
ヒント2の解答例は次のようになる。
(define heron (lambda (a b c)
(define s (/ (+ a b c) 2))
(sqrt (* s (- s a) (- s b) (- s c)))))
|
ヒント3の解答例は次のようになる。よく見ると、ヒント1の解答例にて、heron4 の定義を展開しただけである。
(define heron (lambda (a b c)
((lambda (a b c s) (sqrt (* s (- s a) (- s b) (- s c))))
a b c (/ (+ a b c) 2))))
|