開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: MIT/GNU Scheme
計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション)の2(データによる抽象の構築)、2.1(データ抽象入門)、2.1.2(抽象の壁)の問題 2.2、問題 2.3を解いてみる。
その他参考書籍
問題 2.2
コード
sample.scm
(define (make-segment a b) (cons a b)) (define (make-point x y) (cons x y)) (define (start-segument segment) (car segment)) (define (end-segment segment) (cdr segment)) (define (x-point p) (car p)) (define (y-point p) (cdr p)) (define (midpoint-segment segment) (define a (start-segument segment)) (define b (end-segment segment)) (define x1 (x-point a)) (define y1 (y-point a)) (define x2 (x-point b)) (define y2 (y-point b)) (make-point (/ (+ x1 x2) 2) (/ (+ y1 y2) 2))) (define (print-point p) (newline) (display "(") (display (x-point p)) (display ",") (display (y-point p)) (display ")")) (define a (make-point 0 0)) (define b (make-point 10 0)) (define c (make-point 0 10)) (define d (make-point 10 10)) (define s1 (make-segment a b)) (define s2 (make-segment a c)) (define s3 (make-segment a d)) (define s4 (make-segment b c)) (define s5 (make-segment b d)) (define s6 (make-segment c d)) (define m1 (midpoint-segment s1)) (define m2 (midpoint-segment s2)) (define m3 (midpoint-segment s3)) (define m4 (midpoint-segment s4)) (define m5 (midpoint-segment s5)) (define m6 (midpoint-segment s6))
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
1 ]=> (print-point m1) (5,0) ;Unspecified return value 1 ]=> (print-point m2) (0,5) ;Unspecified return value 1 ]=> (print-point m3) (5,5) ;Unspecified return value 1 ]=> (print-point m4) (5,5) ;Unspecified return value 1 ]=> (print-point m5) (10,5) ;Unspecified return value 1 ]=> (print-point m6) (5,10) ;Unspecified return value
問題 2.3
コード
sample.scm
(define (make-segment a b) (cons a b)) (define (make-point x y) (cons x y)) (define (start-segument segment) (car segment)) (define (end-segment segment) (cdr segment)) (define (x-point p) (car p)) (define (y-point p) (cdr p)) ; 4点から長方形を作成 (define (make-rectangle-point a b c d) (cons a (cons b (cons c d)))) ; 直交する2つのベクトルから長方形を作成 (define (make-rectangle-vector v1 v2) (define a (make-point 0 0)) (define b (make-point (x-point v1) (y-point v1))) (define c (make-point (+ (x-point v1) (x-point v2)) (+ (y-point v1) (y-point v2)))) (define d (make-point (x-point v2) (y-point v2))) (cons a (cons b (cons c d)))) (define (make-rectangle-segment-ab r) (make-segment (rectangle-a r) (rectangle-b r))) (define (make-rectangle-segment-bc r) (make-segment (rectangle-b r) (rectangle-c r))) (define (make-rectangle-segment-cd r) (make-segment (rectangle-c r) (rectangle-d r))) (define (rectangle-a r) (car r)) (define (rectangle-b r) (car (cdr r))) (define (rectangle-c r) (car (cdr (cdr r)))) (define (rectangle-d r) (cdr (cdr (cdr r)))) (define (length-segment segment) (define a (start-segument segment)) (define b (end-segment segment)) (define ax (x-point a)) (define bx (x-point b)) (define ay (y-point a)) (define by (y-point b)) (sqrt (+ (square (- ax bx)) (square (- ay by))))) (define (perimeter-rectangle rectangle) (define segment-ab (make-rectangle-segment-ab rectangle)) (define segment-bc (make-rectangle-segment-bc rectangle)) (define length-segment-ab (length-segment segment-ab)) (define length-segment-bc (length-segment segment-bc)) (* 2 (+ length-segment-ab length-segment-bc))) (define (area-rectangle rectangle) (define segment-ab (make-rectangle-segment-ab rectangle)) (define segment-bc (make-rectangle-segment-bc rectangle)) (define length-segment-ab (length-segment segment-ab)) (define length-segment-bc (length-segment segment-bc)) (* length-segment-ab length-segment-bc)) (define (make-vector a) (cons (car a) (cdr a))) ; 4点を指定して長方形を作成 (define rec-1-p (make-rectangle-point (make-point 0 0) (make-point 0 10) (make-point 10 10) (make-point 10 0))) (define rec-2-p (make-rectangle-point (make-point -5 6) (make-point -10 1) (make-point 5 -15) (make-point 10 -10))) ; 2つのベクトルで上記の2つの長方形と同じ長方形を作成 (define rec-1-v (make-rectangle-vector (make-vector (make-point 0 10)) (make-vector (make-point 10 0)))) (define rec-2-v (make-rectangle-vector (make-vector (make-point 5 5)) (make-vector (make-point 15 -16))))
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
1 ]=> (perimeter-rectangle rec-1-p) ;Value: 40 1 ]=> (perimeter-rectangle rec-1-v) ;Value: 40 1 ]=> (perimeter-rectangle rec-2-p) ;Value: 58.00556002265357 1 ]=> (perimeter-rectangle rec-2-v) ;Value: 58.00556002265357 1 ]=> (area-rectangle rec-1-p) ;Value: 100 1 ]=> (area-rectangle rec-1-v) ;Value: 100 1 ]=> (area-rectangle rec-2-p) ;Value: 155.08062419270823 1 ]=> (area-rectangle rec-2-v) ;Value: 155.08062419270823 1 ]=> ^D End of input stream reached. Moriturus te saluto.
0 コメント:
コメントを投稿