2015/10/10
SICP 問題 2.2
(define (make-segment start end)
(cons start end))
(define (start-segment seg)
(car seg))
(define (end-segment seg)
(cdr seg))
(define (make-point x y)
(cons x y))
(define (x-point point)
(car point))
(define (y-point point)
(cdr point))
(define (midpoint-segment seg)
(let ((s-seg (start-segment seg))
(e-seg (end-segment seg)))
(make-point (/ (+ (x-point s-seg)
(x-point e-seg))
2)
(/ (+ (y-point s-seg)
(y-point e-seg))
2))))
印字用手続きで確かめてみる.
(define (print-point p)
(newline)
(display "(")
(display (x-point p))
(display ",")
(display (y-point p))
(display ")"))
gosh> (define my-segment (make-segment '(0 . 0) '(2 . 2)))
my-segment
gosh> (cdr my-segment)
(2 . 2)
gosh> (car my-segment)
(0 . 0)
gosh> (print-point my-segment)
((0 . 0),(2 . 2))#<undef>
gosh> (print-point (midpoint-segment my-segment))
(1,1)#<undef>