In sections
,
,
, and
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.
Exercise 41.4.1
Turn translate-circle and translate-rectangle of
exercises
and
, respectively, into
structure-mutating the functions. Adapt move-circle, defined in
section
, and move-rectangle from
exercise
so that they use these new
functions. Solution
Exercise 41.4.2
Adapt the function move-picture from exercise
to
use the structure-mutating functions from
exercise
. Solution
Exercise 41.4.3
Use Scheme's for-each function (see Help Desk) to abstract where
possible in the functions of
exercise
. Solution