### Assignment 7: Generative Recursion (due Oct 27)

Before you tackle the homework, remind yourself of our General Advice, Advice on Homeworks, and Grading Guidelines. Above all, keep your work neat and honest.

As of this week, you do not need to provide templates with your solutions. However, your solutions must still follow the proper templates and design recipes (whether your program uses structural or generative recursion)

1. (3 pts) Exercise 27.5.9 (page 391).

2. (3 pts) Exercise 27.1.5 describes a process (due to Bezier) for drawing curved lines between two points. Write a program bezier which consumes three posn structures (for the vertices of a triangle) and returns a list of posn structures. The returned list represents the points that form the curved line between the first and third points (do not include the first and third points in the returned list).

To test your program, set the library to draw-lib.ss (this is one of the built-in DrScheme libraries). The following code provides a function draw-curve, which consumes three posns and draws (a) the triangle and (b) the curve between the first and third posns. The function draw-curve calls bezier to get the list of intermediate points. Copy this code into your file, and test your program by calling draw-curve. The text for exercise 27.1.5 provides one sample input. Make up at least two more to test your program.

The data definition for a posn is in draw-lib.ss. Don't type it into your code.

;; draw-lop : (listof posn) -> true
;; draws a BLUE line between each consecutive pair of points in the list
(define (draw-lop alop)
(cond [(empty? (rest alop)) true]
[else (and (draw-solid-line (first alop) (first (rest alop)) BLUE)
(draw-lop (rest alop)))]))

;; draw-curve : posn posn posn -> true
;; draws a curve between p1 and p3 based on the points determined by
;;   the bezier function
(define (draw-curve p1 p2 p3)
(local ((define points (bezier p1 p2 p3)))
(and (draw-solid-line p1 p2)
(draw-solid-line p2 p3)
(draw-solid-line p1 p3)
(draw-lop (append (list p1) points (list p3))))))

;; be sure to include this line -- it creates a canvas in which the
;;  drawing commands are executed.
(start 300 200)

3. (5 pts) Exercise 27.3.7 (page 382).

Note: The exercise text refers to the divide-and-conquer strategy used in the problem find-root; this problem appears earlier in section 27.3. Basically, the strategy is as follows: if the distance between the two points is below some small threshold (choose something reasonable for the threshold), stop dividing the problem into pieces. Otherwise, divide the problem into two pieces around the point halfway between the two points that the program is currently considering.

 Kathi Fisler This page was generated on Wed Oct 20 12:18:11 CDT 1999.