-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.03.scm
41 lines (34 loc) · 1.09 KB
/
2.03.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(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 p1 p2) (cons p1 p2))
(define (start-segment s) (car s))
(define (end-segment s) (cdr s))
(define (segment-length s)
(define (square n)
(* n n))
(sqrt (+ (square (abs (- (x-point (start-segment s))
(x-point (end-segment s)))))
(square (abs (- (y-point (start-segment s))
(y-point (end-segment s))))))))
(define (make-rectangle s1 s2) (cons s1 s2))
(define (rect-width r) (segment-length (car r)))
(define (rect-height r) (segment-length (cdr r)))
(define (rect-perimeter r)
(* 2 (+ (rect-width r) (rect-height r))))
(define (rect-area r)
(* (rect-width r) (rect-height r)))
(let* ((p1 (make-point 0 0))
(p2 (make-point 2 0))
(p3 (make-point 0 2))
(s1 (make-segment p1 p2))
(s2 (make-segment p1 p3))
(r (make-rectangle s1 s2)))
(rect-perimeter r))