On this page:
From Unions to Recursive Unions
Extending the World
A Challenge
6.10.1

Lab 4: Unions and Recursion

Important Note Whenever we say to "design a function", we mean that you need to follow the design recipe. Any other time that you write a function in this class, you also need to follow the design recipe.

From Unions to Recursive Unions

Recall unions of structures from class.

Exercise 1 Write the template for a function which processes the following data and structure definitions:
; A Frosting is one of:
;  - "chocolate"
;  - "vanilla"
 
; A Dessert is one of:
;  - (make-cupcake Frosting)
;  - (make-pie String Number)
 
(define-struct cupcake (frosting))
(define-struct pie (filling slices))

Exercise 2 Write three examples of Desserts.

Exercise 3 Design a function which computes how many calories are in a Dessert. A chocolate cupcake has 150 calories, and a vanilla cupcake has 125 calories. A single slice of pie has 175 calories. Follow the template you wrote above, and use the examples you have defined in your tests.

Now we want to model a case of many desserts at a pastry shop. A Case of Desserts might be empty, or it might contain a single dessert alongside the rest of the case.

Exercise 4 Develop a data definition and corresponding structure definitions for a Case of Desserts.

Exercise 5 Write three examples of Cases of Desserts.

Exercise 6 Write the template for processing a Case of Desserts.

Exercise 7 Design the function total-calories, which takes a Case of Desserts as input and produces the total calories for all the desserts in the case. Follow the template you wrote above, and use the examples you have defined in your tests.

Extending the World

We would like to create a program using big-bang which initially draws a blank screen, but when you click the mouse (which produces the MouseEvent "button-down") it draws a circle at that position. When you click again, it should draw a new circle at the new position, and the old circle should disappear.

Exercise 8 Design a data definition and structure definitions for representing the World in this program.

Exercise 9 Write three examples of Worlds.

Exercise 10 Write the template for processing the World data definition that you have defined.

Exercise 11 Design the appropriate to-draw and on-mouse functions for this big-bang program. Hint: you don’t need an on-tick function.

A Challenge

Now apply the lessons from the first part of the lab to your big-bang program.

Semi-Challenge Exercise Modify the program from Exercise 10 so that instead of the old circle disappearing when you click the mouse, new circles appear each time, so that your world has more and more circles over time. This should be very similar to the program we wrote in class.

Challenge Exercise Modify the program from the previous challenge so that instead of the circles staying where they start, they all move in some direction after being created.