Last updated: Mon, 20 Apr 2015 16:40:05 -0400
- Note: These slides are from a previous semester. We provide them because learning is often aided by looking at different presentations of the same material. However, previous iterations of the course had a different structure and different style requirements. Read these slides for their ideas only. When there are discrepancies, follow the rules of our current course.
Understand how to use mutable objects.
Understand the advantages and disadvantages of using mutation.
Understand some common patterns that require mutable objects.
The following big-bang program displays a ball bouncing inside a box (start with (run)).
The box may be resized by clicking and dragging on its right edge. However, the ball does not change behavior when the box is resized because the sharing of the Box% object is lost when the World% creates a new Box% but does not update the Ball%.
Fix this program by using mutation. Use either the "push" or "pull" style, as described in the course notes. If you go with the "push" approach, define explicit Subscriber<%> and Publisher<%> interfaces. Be ready to justify your design choice.
Make sure your code follows the Effectful Methods style guide.
Here are some Racket functions and forms that may be useful in effectful programs:
Also, some Racket forms have an implicit begin in their bodies:
This means that the following f and g functions are the same.
Solution 1 (pull):
Solution 2 (push):