Lab G -- Toppers catchup for modules F or earlier

CISC 280 Program Development Techniques

Top off your modules in order. If you haven't topped D, or E, or F do the appropriate ones of these first. If you just feel like having the practice, do these after topperG.

Module F topping off exercise, topperF3:

Workers at CGU (Computer Graphics Unlimited, Inc.) have a scanner which processes a scanner image of architectural drawings and spits out a list of corner points in the drawing. This list is then fed to the CCC (concrete corner cruncher) which calculates the amount of concrete will be needed for the foundation. The problem is that the scanner output is a list in the form of a sequence of points ((x1 y1) (x2 y2) (x3 y3) ... ), whereas the CCC requires a list of alternating x- and y-coordinates, (x1 y1 x2 y2 x3 y3 ... ), Your job is to translate from the list of points format to the list of numbers format.

Write a procedure (coords->points L) which takes L, a list of points, and returns a list, twice as long, of numbers, where each two successive numbers are the x and y coordinates of a point from the input list. Define and use these basic point procedures make-point, x-coord-point, and y-coord-point:

(define (make-point x y) (list x y))
(define x-coord-point car)
(define y-coord-point cadr)
Note: This bit of good object orientation is a required element in this exercise. Do not use cons. car, cdr, when working with points. On the other hand, when working with the input list, the first element is (car L), and the second is (cadr L)). It would be inappropriate to apply x-coord-point or y-coord-point to L. Remark: You don't need to make points, so probably won't use the make-point function. If you don't need it, you don't have to define it. If you do use it, define it.

For example,
(coords->points '( (1 2) (3 4) (5 6) (7 8) )) returns (1 2 3 4 5 6 7 8).
(coords->points (list (make-point 5.3 2.223))) returns (5.3 2.223).
(coords->points null) returns ().

Hint. Choose wisely among cons, list, append to make this very simple.


Module E topping off exercise, topperE3: Ben Bitdiddle has been working with interval arithmetic. He stores intervals as a pair of endpoints. Thus his constructor is
(define (make-interval lo hi) (cons lo hi))
and his selectors are
(define (lower-bound i) (car i))
(define (upper-bound i) (cdr i))
(define (center i) (/ (+ (lower-bound i) (upper-bound i)) 2)); average them
(define (width i) (- (center i) (lower-bound i))); it is half the full width.

Because the application uses many more calls to center and width than calls to upper-bound and lower-bound, it has been decided for efficiency to redefine the implementation so that the center and the width are stored rather than the endpoints. If c is the center and w is the width, then lower-bound is c-w and upper-bound is c+w. Also it has become company policy to always use proper lists, so a pair of numbers is no longer acceptable. Instead the center and width will have to be stored in a proper list of length 2. Note that make-interval still takes the lower bound and upper bound as the parameters. For example:

(define i (make-interval 10 20)) 
i -> (15 5)
(lower-bound i) -> 10
(upper-bound i) -> 20
(center i) -> 15
(width i) -> 5
Redefine the five functions so that it works this way.
Module D topping off exercise, topperD5:

Write a procedure (one-or-other p f g) which takes a predicate p and two functions f and g. It returns a function, let's call it h for the sake of the discussion, such that h(x) = f(x), if p(x) is true, but h(x) = g(x), if p(x) is false.

Thus when your function is done, I could do this:

(define h (one-or-other even? square cube))
(h 2) -> 4
(h 3) -> 27
(h 4) -> 16
(h 5) -> 125
(define ident (lambda (x) x))
(define negate (lambda (x) (- x)))
(define abs (one-or-other positive? ident negate))
(abs 2) -> 2
(abs -2) -> 2