; ex 2.2

; point
(define (make-point x y) (cons x y))
(define (x-point p) (car p))
(define (y-point p) (cdr p))
(define (point2str p)
  (format "(~a,~a)"
          (x-point p) (y-point p)))
(define (print-point p)
  (newline)
  (display (point2str p)))

; segment
(define (make-segment p1 p2) (cons p1 p2))
(define (start-segment s) (car s))
(define (end-segment s) (cdr s))
(define (segment2str s)
  (format "~a->~a"
          (point2str (start-segment s))
          (point2str (end-segment s))))
(define (print-segment s)
  (newline)
  (display (segment2str s)))

(define (midpoint-segment s)
  (define (average a b) (/ (+ a b) 2))
  (let ((p1 (start-segment s))
        (p2 (end-segment s)))
    (make-point (average (x-point p1) (x-point p2))
                (average (y-point p1) (y-point p2)))))

; ex 2.3

; rect

(define (make-rect tl-point br-point) (cons tl-point br-point))
;(define (make-rect p1 p2) (cons p1 p2))
(define (top-left-rect r) (car r))
(define (top-right-rect r)
  (make-point (x-point (cdr r)) (y-point (car r))))
(define (bottom-left-rect r)
  (make-point (x-point (car r)) (y-point (cdr r))))
(define (bottom-right-rect r) (cdr r))
;(define (top-left-rect r)
;  (let ((x1 (x-point (car r)))
;        (y1 (y-point (car r)))
;        (x2 (x-point (cdr r)))
;        (y2 (y-point (cdr r))))
;    (make-point (min x1 x2) (max y1 y2))))
;(define (top-right-rect r)
;  (let ((x1 (x-point (car r)))
;        (y1 (y-point (car r)))
;        (x2 (x-point (cdr r)))
;        (y2 (y-point (cdr r))))
;    (make-point (max x1 x2) (max y1 y2))))
;(define (bottom-left-rect r)
;  (let ((x1 (x-point (car r)))
;        (y1 (y-point (car r)))
;        (x2 (x-point (cdr r)))
;        (y2 (y-point (cdr r))))
;    (make-point (min x1 x2) (min y1 y2))))
;(define (bottom-right-rect r)
;  (let ((x1 (x-point (car r)))
;        (y1 (y-point (car r)))
;        (x2 (x-point (cdr r)))
;        (y2 (y-point (cdr r))))
;    (make-point (max x1 x2) (min y1 y2))))
(define (rect2str r)
  (format "tl=~a\ntr=~a\nbl=~a\nbr=~a"
          (top-left-rect r)
          (top-right-rect r)
          (bottom-left-rect r)
          (bottom-right-rect r)))
(define (print-rect r)
  (newline)
  (display (rect2str r)))

(define (width-rect r)
  (- (x-point (top-right-rect r)) (x-point (top-left-rect r))))
(define (height-rect r)
  (- (y-point (top-left-rect r)) (y-point (bottom-left-rect r))))
(define (area-rect r)
  (* (width-rect r) (height-rect r)))
(define (girth-rect r)
  (+ (* (width-rect r) 2) (* (height-rect r) 2)))
신고

2.1.2

Announce/Chapter.2 2011.01.01 11:07
...
신고

; ex_2_1

(define (make-rat3 n d)
  (let ((g ((if (< d 0) - +) (gcd n d))))
    (cons (/ n g) (/ d g))))
신고

티스토리 툴바