Blame


1 665c255d 2023-08-04 jrmu ;; Exercise 2.2. Consider the problem of representing line segments in a plane. Each segment is represented as a pair of points: a starting point and an ending point. Define a constructor make-segment and selectors start-segment and end-segment that define the representation of segments in terms of points. Furthermore, a point can be represented as a pair of numbers: the x coordinate and the y coordinate. Accordingly, specify a constructor make-point and selectors x-point and y-point that define this representation. Finally, using your selectors and constructors, define a procedure midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose coordinates are the average of the coordinates of the endpoints). To try your procedures, you'll need a way to print points:
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu (define (make-point x y)
4 665c255d 2023-08-04 jrmu (cons x y))
5 665c255d 2023-08-04 jrmu (define (x-point p)
6 665c255d 2023-08-04 jrmu (car p))
7 665c255d 2023-08-04 jrmu (define (y-point p)
8 665c255d 2023-08-04 jrmu (cdr p))
9 665c255d 2023-08-04 jrmu
10 665c255d 2023-08-04 jrmu (define (make-segment start end)
11 665c255d 2023-08-04 jrmu (cons start end))
12 665c255d 2023-08-04 jrmu (define (start-segment seg)
13 665c255d 2023-08-04 jrmu (car seg))
14 665c255d 2023-08-04 jrmu (define (end-segment seg)
15 665c255d 2023-08-04 jrmu (cdr seg))
16 665c255d 2023-08-04 jrmu (define (midpoint-segment seg)
17 665c255d 2023-08-04 jrmu (define (average x y)
18 665c255d 2023-08-04 jrmu (/ (+ x y) 2))
19 665c255d 2023-08-04 jrmu (let ((x1 (x-point (start-segment seg)))
20 665c255d 2023-08-04 jrmu (x2 (x-point (end-segment seg)))
21 665c255d 2023-08-04 jrmu (y1 (y-point (start-segment seg)))
22 665c255d 2023-08-04 jrmu (y2 (y-point (end-segment seg))))
23 665c255d 2023-08-04 jrmu (make-point (average x1 x2)
24 665c255d 2023-08-04 jrmu (average y1 y2))))
25 665c255d 2023-08-04 jrmu
26 665c255d 2023-08-04 jrmu (define (print-point p)
27 665c255d 2023-08-04 jrmu (newline)
28 665c255d 2023-08-04 jrmu (display "(")
29 665c255d 2023-08-04 jrmu (display (x-point p))
30 665c255d 2023-08-04 jrmu (display ",")
31 665c255d 2023-08-04 jrmu (display (y-point p))
32 665c255d 2023-08-04 jrmu (display ")"))
33 665c255d 2023-08-04 jrmu
34 665c255d 2023-08-04 jrmu (define x1y2 (make-point 1 2))
35 665c255d 2023-08-04 jrmu (define x-4y-3 (make-point -4 -3))
36 665c255d 2023-08-04 jrmu (define x1y2tox-4y-3 (make-segment x1y2 x-4y-3))
37 665c255d 2023-08-04 jrmu (print-point (midpoint-segment x1y2tox-4y-3))
38 665c255d 2023-08-04 jrmu (display "=(-3/2,-1/2)")
39 665c255d 2023-08-04 jrmu
40 665c255d 2023-08-04 jrmu ;; Exercise 2.3. Implement a representation for rectangles in a plane. (Hint: You may want to make use of exercise 2.2.) In terms of your constructors and selectors, create procedures that compute the perimeter and the area of a given rectangle. Now implement a different representation for rectangles. Can you design your system with suitable abstraction barriers, so that the same perimeter and area procedures will work using either representation?
41 665c255d 2023-08-04 jrmu
42 665c255d 2023-08-04 jrmu ;; makes rectangle given 4 points: top-left, top-right, bottom-left, bottom-right
43 665c255d 2023-08-04 jrmu ;; (define (make-rect tl tr bl br)
44 665c255d 2023-08-04 jrmu ;; (cons (cons tl tr)
45 665c255d 2023-08-04 jrmu ;; (cons bl br)))
46 665c255d 2023-08-04 jrmu ;; (define (top-left rect)
47 665c255d 2023-08-04 jrmu ;; (caar rect))
48 665c255d 2023-08-04 jrmu ;; (define (top-right rect)
49 665c255d 2023-08-04 jrmu ;; (cdar rect))
50 665c255d 2023-08-04 jrmu ;; (define (bot-left rect)
51 665c255d 2023-08-04 jrmu ;; (cadr rect))
52 665c255d 2023-08-04 jrmu ;; (define (bot-right rect)
53 665c255d 2023-08-04 jrmu ;; (cddr rect))
54 665c255d 2023-08-04 jrmu
55 665c255d 2023-08-04 jrmu ;; makes rectangle given 2 points: top-left and bottom-right
56 665c255d 2023-08-04 jrmu (define (make-rect tl br)
57 665c255d 2023-08-04 jrmu (let ((tr (make-point (x-point br) (y-point tl)))
58 665c255d 2023-08-04 jrmu (bl (make-point (x-point tl) (y-point br))))
59 665c255d 2023-08-04 jrmu (cons (cons tl tr)
60 665c255d 2023-08-04 jrmu (cons bl br))))
61 665c255d 2023-08-04 jrmu (define (top-left rect)
62 665c255d 2023-08-04 jrmu (caar rect))
63 665c255d 2023-08-04 jrmu (define (bot-right rect)
64 665c255d 2023-08-04 jrmu (cddr rect))
65 665c255d 2023-08-04 jrmu (define (top-right rect)
66 665c255d 2023-08-04 jrmu (cdar rect))
67 665c255d 2023-08-04 jrmu (define (bot-left rect)
68 665c255d 2023-08-04 jrmu (cadr rect))
69 665c255d 2023-08-04 jrmu
70 665c255d 2023-08-04 jrmu
71 665c255d 2023-08-04 jrmu
72 665c255d 2023-08-04 jrmu (define (perimeter rect)
73 665c255d 2023-08-04 jrmu