Chapter 2
2.1 Exercise 2.1
2.2 Exercise 2.2
2.3 Exercise 2.3
2.4 Exercise 2.4
2.5 Exercise 2.5
2.6 Exercise 2.6
2.7 Exercise 2.7
2.8 Exercise 2.8
2.9 Exercise 2.9
2.10 Exercise 2.10
2.11 Exercise 2.11
2.12 Exercise 2.12
2.13 Exercise 2.13
2.14 Exercise 2.14
2.15 Exercise 2.15
2.16 Exercise 2.16
2.17 Exercise 2.17
2.18 Exercise 2.18
2.19 Exercise 2.19
2.20 Exercise 2.20
2.21 Exercise 2.21
2.22 Exercise 2.22
2.23 Exercise 2.23
2.24 Exercise 2.24
2.25 Exercise 2.25
2.26 Exercise 2.26
2.27 Exercise 2.27
2.28 Exercise 2.28
2.29 Exercise 2.29
2.30 Exercise 2.30
2.31 Exercise 2.31
2.32 Exercise 2.32
2.33 Exercise 2.33
2.34 Exercise 2.34
2.35 Exercise 2.35
2.36 Exercise 2.36
2.37 Exercise 2.37
2.38 Exercise 2.38
2.39 Exercise 2.39
2.40 Exercise 2.40
2.41 Exercise 2.41
2.42 Exercise 2.42
2.43 Exercise 2.43
2.44 Exercise 2.44
2.45 Exercise 2.45
2.46 Exercise 2.46
2.47 Exercise 2.47
2.48 Exercise 2.48
2.49 Exercise 2.49
2.50 Exercise 2.50
2.51 Exercise 2.51
2.52 Exercise 2.52
2.53 Exercise 2.53
2.54 Exercise 2.54
2.55 Exercise 2.55
2.56 Exercise 2.56
2.57 Exercise 2.57
2.58 Exercise 2.58
2.59 Exercise 2.59
2.60 Exercise 2.60
2.61 Exercise 2.61
2.62 Exercise 2.62
2.63 Exercise 2.63
2.64 Exercise 2.64
2.65 Exercise 2.65
2.66 Exercise 2.66
2.67 Exercise 2.67
2.68 Exercise 2.68
2.69 Exercise 2.69
2.70 Exercise 2.70
2.71 Exercise 2.71
2.72 Exercise 2.72
2.73 Exercise 2.73
2.74 Exercise 2.74
2.75 Exercise 2.75
2.76 Exercise 2.76
2.77 Exercise 2.77
2.78 Exercise 2.78
2.79 Exercise 2.79
2.80 Exercise 2.80
2.81 Exercise 2.81
2.82 Exercise 2.82
2.83 Exercise 2.83
2.84 Exercise 2.84
2.85 Exercise 2.85
2.86 Exercise 2.86
2.87 Exercise 2.87
2.88 Exercise 2.88
2.89 Exercise 2.89
2.90 Exercise 2.90
2.91 Exercise 2.91
2.92 Exercise 2.92
2.93 Exercise 2.93
2.94 Exercise 2.94
2.95 Exercise 2.95
2.96 Exercise 2.96
2.97 Exercise 2.97

2.2 Exercise 2.2

The procedures for making and selecting from line segments and points are very similar – just operations on pairs of their parts. make-segment and make-point are both calls to cons, start-segment and x-point are both cars, and end-segment and y-point are both cdrs.

(define (make-segment start end)
  (cons start end))
(define (start-segment segment)
  (car segment))
(define (end-segment segment)
  (cdr segment))
(define (make-point x y)
  (cons x y))
(define (x-point point)
  (car point))
(define (y-point point)
  (cdr point))

midpoint-segment is a simple operation relying on an average procedure that is trivial enough to not include here.

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

To illustrate the power of these similar means of abstraction even more, we can generalize the print-point procedure. The original as listed in the exercise is as follows:

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

We could implement a separate print-segment procedure that calls print-point, but I believe we can do better. print-point can be imagined as an instance of a process doing the following things:

Therefore, we could write a generalized print-pair procedure and implement print-point, and also a new procedure print-segment, with it.

(define (print-pair pair pre between end inner-print)
  (display pre)
  (display (car pair))
  (display between)
  (display (cdr pair))
  (display end))

However, while this will work perfectly well for reimplementing print-point, it works less than well for making print-segment. It would be desirable to have print-segment call print-point on each of its points, but print-pair will necessarily call display on them, printing them with the environment’s default representation for pairs. This will give a readable printout, but I believe we should do better. This can be done by supplying one more argument to print-pair, an inner-print procedure that is called to print the car and cdr of the pair And so we get the actual print-pair procedure:

(define (print-pair pair pre between end inner-print)
  (display pre)
  (inner-print (car pair))
  (display between)
  (inner-print (cdr pair))
  (display end))

And with this, we can make print-point and print-segment:

(define (print-point p)
  (print-pair p "(" "," ")" display))
(define (print-segment s)
  (print-pair s "" " -> " "" print-point))

There is another benefit that we can get out of this. Suppose we implemented print-segment using the first version of print-point given in the book. Each point would have a newline before it, necessarily and always. If we wanted to change this (say, to print a segment on one line), we would have to write a new print-point procedure. This is significantly easier to handle with the new print-pair procedure – we can simply supply newlines to be printed wherever we want. The problem could be solved like so:

(define (println-segment s)
  (print-pair s "" " -> " "\n" print-point))

It would be even better if the cars and cdrs of the pairs sent to print-pair knew how to print themselves, but that is getting beyond the scope of the exercise.