[previous] [up] [next]     [index]
Next: Equality Up: Designing Functions that Change Previous: Structural Design Recipes and

Extended Exercise: Moving Pictures, a Last Time

In sections [cross-reference], [cross-reference], [cross-reference], and [cross-reference] we studied how to move pictures across a canvas. A picture is a list of shapes; a shape is one of several basic geometric shapes: circles, rectangles, etc. Following our most basic design principle--one function per concept--we first defined functions for moving basic geometric shapes, then for mixed classes of shapes, and finally for lists of shapes. Eventually we abstracted over related functions.

The functions for moving basic shapes create a new shape from an existing shape. For example, the function for moving a circle consumes a circle structure and produces a new circle structure. If we think of the circle as a painting with a round frame and the canvas as a wall, however, creating a new shape for each move is inappropriate. Instead, we should change the shape's current position.


Exercises

Exercise 41.4.1

Turn translate-circle and translate-rectangle of exercises [cross-reference] and [cross-reference], respectively, into structure-mutating the functions. Adapt move-circle, defined in section [cross-reference], and move-rectangle from exercise [cross-reference] so that they use these new functions. Solution

Exercise 41.4.2

Adapt the function move-picture from exercise [cross-reference] to use the structure-mutating functions from exercise [cross-reference]Solution

Exercise 41.4.3

Use Scheme's for-each function (see Help Desk) to abstract where possible in the functions of exercise [cross-reference]Solution




PLT