2.2(点,线段,矩形的构造)

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

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

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

(define (print-point p)
  (newline)
  (display "{")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display "}"))

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

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

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

(define (average x y)
  (/ (+ x y)
     2))

(define (midpoint-segment l)
  (let ((start (start-segment l))
        (end (end-segment l)))
    (make-point (average (x-point start)
                         (x-point end))
                (average (y-point start)
                         (y-point end)))))


(print-point (make-point 1 1))

(define start (make-point 1 3))
(define end (make-point 4 3))
(define seg (make-segment start end))
(define mid (midpoint-segment seg))
(print-point mid)


(define (perimeter-rectangle sq)
  (let ((length (length-of-rectangle sq))
        (width (width-of-rectangle sq)))
    (* 2
       (+ length width))))

(define (area-rectangle sq)
  (* (length-of-rectangle sq)
     (width-of-rectangle sq)))
;
(define (make-rectangle length-1 length-2 width-1 width-2)
    (cons (cons length-1 length-2)
          (cons width-1 width-2)))

;; selector

(define (length-1-rectangle r)
    (car (car r)))

(define (length-2-rectangle r)
    (cdr (car r)))

(define (width-1-rectangle r)
    (car (cdr r)))

(define (width-2-rectangle r)
    (cdr (cdr r)))

 

posted @ 2018-01-31 22:44  R4mble  阅读(142)  评论(0编辑  收藏  举报