SICP 2.2 문제 답

cons 는 데이터 구조 에 있어 서 매우 강 한 것 이다.
(define (print-point p)
  (newline)
  (display "(")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display ")"))

(define (make-point x y)
  (cons x y))

(define (x-point p)
  (car p))

(define (y-point p)
  (cdr p))

(define (make-segment start end)
  (cons start end))

(define (start-segment segment)
  (car segment))

(define (end-segment segment)
  (cdr segment))

(define (midpoint-segment segment)
  (cons (/ (+ (x-point (start-segment segment)) (x-point (end-segment segment))) 2)
        (/ (+ (y-point (start-segment segment)) (y-point (end-segment segment))) 2)))

(define p1 (make-point 1 2))
(define p2 (make-point 1 4))
(define s (make-segment p1 p2))

(x-point p1)
(y-point p1)
(print-point p1)
(print-point (midpoint-segment s))

좋은 웹페이지 즐겨찾기