CS 5010 F '09 Pair Programming The Recipes The Style Subversion Assignments Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set 11 Set 12

### Problem Set 7

Due date: 11/04

Purpose:

The primary goal of this problem set is to practice design with and without accumulator

#### Drill:

HtDP: 31.3.1, 31.3.2, 31.3.3, 31.3.4, 31.3.5, 31.3.6, 31.3.7, 31.3.8, 32.2: all exercises or 32.3: all exercises

#### Required Problems:

Note: You must annotate each function with the design strategy that you used as follows:

• domain knowledge exclusively (algebra, physics, watching traffic lights)
• function composition (very few functions qualify!)
• structural design, which argument
• structural design, supplemented with list abstractions (313)
• generative-recursion design.
For both structural and generative designs, you may add "with accumulators" though if you do, be sure to explain the connection between the local recursion argument, the accumulator, and the initial argument.

1. Equip your editor from problem 2.4 with mouse clicks. Specifically, enable the user to place the cursor between two characters by clicking on the mouse, i.e., triggering a `"button-down"` event.

Since each character is several pixels wide, a mouse click is a somewhat ambiguous place for the cursor. After all, a cursor is always placed between two characters. We therefore demand that your program place the cursor between the string prefix `s` of the buffer whose rendering is strictly shorter than the `x` coordinate of the mouse click and string `s` plus one character such that its rendering is longer or equal than `x`. Of course, if there are no characters in the buffer, place the cursor always at the front.

2. Design the function `render` whose purposes it is to turn an `Expr` into an image:

``````     (define-struct pls (x in?))
(define-struct mul (x in?))
;; Expr is one of:
;; -- Number
;; -- (make-pls (cons Expr 1LON) Boolean)
;; -- (make-mul (cons Expr 1LON) Boolean)
;; 1LON is one of:
;; -- (cons Expr empty)
;; -- (cons Expr 1LON)
;; interp. a pls struct represents an addition expression
;; with at least two sub-expressions; a mul represents a
;; multiplication; the boolean flag indicates whether it
;; represents an infix or a prefix expression.
``````
The program consumes an `Expr` and a `Number`, which specifies the permissible width of the expression. Using only `(require htdp/image)` and `(require "image-ops.ss")` it produces an `Image` according to the following algorithm:
1. Both prefix and infix `Expr`s are rendered in parenthetical form if they are compound. For example, ```(make-pls (list 1 2 3) true)``` means your program renders something like ```(1 + 2 + 3)``` and `(make-pls (list 1 2 3) false)` should use the ISL notation `(+ 1 2 3)`.
2. The given `Expr` should be rendered on a single line unless the resulting image is too wide for the specified `width`.
3. If so, render the sub-expressions (if any) in a stacked fashion, with each sub-expression and each operator on a separate "line" of the stack. Then enclose the resulting image in an open parenthesis aligned with the top-most line of the stack of expressions and a closing parenthesis aligned with the bottom-most line.
4. All sub-expressions must fit within the space allotted minus the space for the surrounding parentheses (if any). If any of them don't, recursively apply the stacking algorithm.
The algorithm may determine that the image for the given expression cannot possibly fit within the specified width, in which case the algorithm should signal an appropriate error. Use an 11 point, black font.

For the expression

``````  (define hw-example
(make-pls (list (make-mul (list 1 2) true)
(make-pls (list 3 4) false))
true))
``````
you should see something like this:
 `(render hw-example 120)` `(render hw-example 60)` `(render hw-example 30)`

Do not start from your existing solutions for problems concerning `Expr`s.

To use the "image-ops.ss" teachpack, save it in the SVN directory for problem set 7, and add a require line to your program. Don't forget to commit the teachpack to the SVN repository. You should also ensure that after checking everything in, it runs on alternative computers. Oh, and if you add the string "i would like two extra points" to the solution file for this problem, we will give you those points.

 last updated on Wed Dec 2 17:58:10 EST 2009 generated with PLT Scheme