Exercise2.1~2.16

Exercise2.1

(define (make-rat n d)
  (let* ((g (gcd n d))
         (nn (quotient n g))
         (nd (quotient d g)))
    (if (negative? nd)
      (cons (- nn) (- nd))
      (cons nn nd))))
by iwk

(define (make-rat n d)
  (let ((g (gcd n d)))
    (if (negative? (* n d))
        (cons (- (/ (abs n) g)) (/ (abs d) g))
        (cons (/ (abs n) g) (/ (abs d) g)))))
by alaskan

Exercise2.2

(define (make-point x y)
  (if (and (exact? x) (exact? y))
    (cons x y)
    (error "Argument not integer")))
(define x-point car)
(define y-point cdr)
(define (point? p) (and (pair? p) (exact? (car p)) (exact? (cdr p))))
(define (make-segment start end) 
  (if (and (point? start) (point? end))
    (cons start end)             
    (error "Argument not point")))
(define (segment? seg) (and (pair? seg) (point? (car seg)) (point? (cdr seg))))
(define (start-segment seg)
  (if (segment? seg)
    (car seg)
    (error "Argument not segment")))
(define (end-segment seg)
  (if (segment? seg)
    (cdr seg)
    (error "Argument not segment")))
       
(define (midpoint-segment seg)
  (let* ((start (start-segment seg))
         (end (end-segment seg))
         (xsum (+ (x-point start) (x-point end)))
         (ysum (+ (y-point start) (y-point end))))
    (make-point (quotient xsum 2) (quotient ysum 2))))

(define (print-point p)
  (newline)
  (display "(")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display ")"))

by iwk

Exercise2.3

 ;;; 表現1: 左下の点と右上の点によって長方形を表現
(define (make-rectangle lb rt)
  (cons lb rt))

(define (left-bottom rect)
  (car rect))

(define (right-top rect)
  (cdr rect))

;;; 縦の辺の長さ
(define (height rect)
  (abs (- (y-point (left-bottom rect))
	  (y-point (right-top rect)))))

;;; 横の辺の長さ
(define (width rect)
  (abs (- (x-point (left-bottom rect))
	  (x-point (right-top rect)))))

;;; 表現2: 縦の辺と横の辺によって表現 (辺は線分で表現)
(define (make-rectangle h w)
  (cons h w))

(define (vertical rect)
  (car rect))

(define (horizontal rect)
  (cdr rect))

;;; 縦の辺の長さ
(define (height rect)
  (abs (- (y-point (end-segment (vertical rect)))
	  (y-point (start-segment (vertical rect))))))

;;; 横の辺の長さ
(define (width rect)
  (abs (- (x-point (end-segment (horizontal rect)))
	  (x-point (start-segment (horizontal rect))))))

;;; 周囲の長さ と 面積
(define (perimeter rect)
  (* 2 (+ (height rect) (width rect))))

(define (area rect)
  (* (height rect) (width rect)))
by chrono

Exercise2.4

(define (mycdr z)     
  (z (lambda (p q) q)))
by iwk

Exercise2.5

(define (my-cons a b)
  (* (expt 2 a) (expt 3 b)))

(define (my-car z)
  (if (even? z)
      (+ 1 (my-car (/ z 2)))
      0))

(define (my-cdr z)
  (if (= 0 (remainder z 3))
      (+ 1 (my-cdr (/ z 3)))
      0))
by iriya

Exercise2.6

(define zero (lambda (f) (lambda(x) x)))
(define (add-1 n)
  (lambda (f) (lambda (x) (f ((n f) x)))))

(add-1 zero)
-β->
λ f.λ x.(f ((zero f) x))
-β->
λ f.λ x.(f ((λ y.y) x))
-β->
λ f.λ x (f x)
(define one (lambda (f) (lambda (x) (f x))))

(add-1 one)
-β->
λ f.λ x.(f ((one f) x))
-β->
λ f.λ x.(f ((λ y.(f y)) x))
-β->
λ f.λ x.(f (f x))
(define two (lambda (f) (lambda (x) (f (f x)))))

(define (plus a b)             
  (letrec ((compose (lambda (x y) (lambda (z) (x (y z))))))
    (lambda (f) (compose (a f) (b f)))))

具体的にβ変換してみる
plus one two
-β->
λ f. compose (one f) (two f)
-β->
λ f.λ z.((one f) ((two f) z))
-β->
λ f.λ z.((λx.f x) ((λx.ff x) z))
-β->
λ f.λ z.((λx.f x) (ff z))
-β->
λ f.λ z.(fff z))
=>third
これは加法になっている。

by iwk

Exercise2.7

(define (upper-bound iv) (cdr iv))
(define (lower-bound iv) (car iv))
by iwk

Exercise2.8

(define (sub-interval x y)
  (make-interval (- (lower-bound x) (upper-bound y))
                 (- (upper-bound x) (lower-bound y))))

by iwk

Exercise2.9

The width of an interval is half of the difference between its upper and lower bounds. The width is a measure of the uncertainly of the number specified by the interval. For some arithmetic operations the width of the result of combining two intervals is a function only of the widths of the argument intervals, whereas for others the width of the combination is not a function of the widths of the argument intervals. Show that the width of the sum (of difference) of two intervals is a function only of the widths of the intervals being added (or subtracted). Give examples to show that this is not true for multiplication or division.
一つのintervalのwidthはintervalの上限と下限の差の半分である。そのwidthはintervalによって指定される数の不明確な測度である。いくつかの数値演算において、2つのintervalを組み合わせた結果のwidthはその2つのintervalのwidthできまる関数となる、一方それら以外の数値演算においてはそのようなことは成り立たない。2つのintervalの和(差)のwidthはその加えられた(引かれた)intervalのそれぞれのwidthの関数であることを示せ。さらに積と商においては上記の事柄は成り立たないことを示す例を挙げよ。

(define (width x)
  (/ (- (upper-bound x) (lower-bound x)) 2.0))
(define (add-width x y)
  (width (add-interval x y)))
;; add-width (a,b) (c,d)
;; width (add-interval (a,b) (c,d))
;; width ((+ a c), (+ b d))
;; (/ (- (+ b d) (+ a c)) 2)
;; (/ (+ (- b a) (- d c)) 2)
;; (+ (/ (- b a) 2) (/ (- d c) 2))
;; (+ (width (a,b)) (width (c,d)))
(define (sub-width x y)
  (width (sub-interval x y)))
;; sub-width (a,b) (c,d)
;; width (sub-interval (a,b) (c,d))
;; width ((- b c),(- a d))
;; width ((+ b (- c)),(+ a (- d)))
;; (/ (- (+ b (- c)) (+ a (- d))) 2)
;; (/ (+ (+ b (- c)) (+ (- a) d)) 2)
;; (/ (+ (- a) b (- c) d) 2)
;; (/ (+ (- b a) (- d c)) 2)
;; (- (/ (- b a) 2) (/ (- d c) 2))
;; (- (width (a,b)) (width (c,d)))

よってadd-interval,sub-intervalは+,-の演算に書き換えられた。=> widthの関数となる。
一方mul-intervalは
gosh> (define x (make-interval 1 4))
x
gosh> (define y (make-interval 3 5))
y
gosh> (* (width x) (width y))
10.0
gosh> (width (mul-interval x y))
11.5
と一致しない。=> width の関数とならない
div-intervalは内部でmul-intervalを呼び出しているので同様に成り立たない。

Exercise2.10

(define (div-interval x y)
  (define (span-zero? ival)
    (let ((product (* (upper-bound ival) (lower-bound ival))))
      (and (= ival 0) (negative? ival))))
  (if (span-zero? y)
      (error "the interval spans zero" y)
      (mul-interval x
                    (make-interval (/ 1.0 (upper-bound y))
                                   (/ 1.0 (lower-bound y))))

by iwk

Exercise2.11

Exercise2.12

Exercise2.13

Exercise2.14

Exercise2.15

Exercise2.16

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2008年03月23日 20:31
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。