Chapter 1.1
Exercise 1.1.
10
;=> 10
(+ 5 3 4)
;=> 12
(- 9 1)
;=> 8
(/ 6 2)
;=> 3
(+ (* 2 4) (- 4 6))
;=> 6
(define a 3)
(define b (+ a 1))
(+ a b (* a b))
;=> 19
(= a b)
;=> #f
(if (and (> b a) (< b (* a b)))
b
a)
;=> 4
(cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25))
;=> 16
(+ 2 (if (> b a) b a))
;=> 6
(* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1))
;=> 16
Exercise 1.2.
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7)))
Exercise 1.3.
(define (f x y z)
(if (> x y) (+ (square x) (square (max y z)))
(f y x z)))
(define (square x) (* x x))
Exercise 1.4
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
;;
(a-plus-abs-b 10 -3)
;; Applicative evaluation
((if #f + -) 10 -3))
;;
(- 10 -3)
13
;; Normal
((if (> -3 0) + -) 10 -3))
((if #f + -) 10 -3))
(- 10 -3)
13
Exercise1.5
(test 0 (p))
;; Applicative
(test 0 ;*evaluate(p)*) == (test 0 (p))
(test 0 (p))
(test 0 (p))
...
;; Normal
(test 0 (p))
(if (= 0 0) 0 (p))
(if #t 0 (p))
0
Exercise1.6
(define (new-test x y) (new-if (= x 0) 0 y))
(define (p) (p))
(new-test 0 (p))
;; Normal
(new-if (= 0 0) 0 (p))
(cond ((= 0 0) 0)
(else (p))))
0
;;
1.7
- 大きな数字の場合に, 接線の傾きが無限大になる = y軸とほぼ平行になるから処理が終わらない
- 小さすぎるときは, 接線の傾きがx軸とほぼ平行になるから, 次回が大きな数字から始まることになるから処理が終わらない
1.8
>(define (cube-iter guess x)
(if (good-enough? guess x)
guess
(cube-iter (improve guess x) x)))
> (define (improve y x)
(/ (+ (/ x (* y y)) (+ y y)) 3))
> (define (cube y) (* y y y))
> (define (good-enough? guess x)
(< (abs (- (cube guess) x)) 0.001))
> (define (cbrt x) (cube-iter 1.0 x))