SICP Lite#5復習
会場・受付でばたばたしてしまって、ちゃんと読めなかったので復習します。
2章前文
この章では対を使って、
ex2.1
(define (make-rat n d) (let ((g (abs (gcd n d)))) (if (< d 0) (cons (/ (- n) g) (/ (- d) g)) (cons (/ n g) (/ d g)))))
ex2.2
(define (make-segment start end) (cons start end)) (define (start-segment p) (car p)) (define (end-segment p) (cdr p)) (define (make-point x y) (cons x y)) (define (x-point p) (car p)) (define (y-point p) (cdr p)) (define (midpoint-segment seg) (let ((sp (start-segment seg)) (ep (end-segment seg))) (make-point (/ (+ (x-point sp) (x-point ep)) 2) (/ (+ (y-point sp) (y-point ep)) 2))))
ex2.3
;;縦、横で長方形を表現 (define (make-rectangle h w) (cons h w)) (define (height-rectangle r) (make-segment (car r))) (define (width-rectangle r) (make-segment (cdr r))) (define (length s) (sqrt (+ (square (- (x-point (start-segment s)) (x-point (end-segment s)))) (square (- (x-point (start-segment s)) (x-point (end-segment s)))))) ;;高さ,幅で長方形を表現 (define (make-rectangle h w) (cons h w)) (define (height-rectangle r) (car r)) (define (width-rectangle r) (cdr r)) (define (parimeter r) (+ (* 2 (height-rectangle r)) (* 2 (width-rectangle r)))) (define (area r) (* (height-rectangle r) (width-rectangle r)))
ex2.4
(define (cons x y) (lambda (m) (m x y))) ;;「引数を1つ(m)とり、(m x y)を返す手続き」を返す (define (cdr z) (z (lambda (p q) q)))
ex2.5
(define (ex-cons a b) (* (expt 2 a) (expt 3 b))) (define (pair z value result) (if (= (remainder z value) 0) (pair (/ z value) value (+ result 1)) result)) (define (ex-car z) (pair z 2 0)) (define (ex-cdr z) (pair z 3 0))