On this page:
1 From unions to recursive unions
2 Extending the world
3 Challenges
6.11

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.

1 From unions to recursive unions

Recall unions of structures from class.

Exercise 1 Define three examples of railroad Wagons, according to the following data and structure definitions:
; A Wagon is one of:
;  - (make-passenger-wagon Company)
;  - (make-freight-wagon String Number)
 
; A Company is one of:
;  - "Alstom"
;  - "Bombardier"
 
(define-struct passenger-wagon (model))
(define-struct freight-wagon (destination axles))
Your three examples should make use of every line of these data definitions.

Exercise 2 List the signatures for each of the courtesy functions for the passenger-wagon and freight-wagon structures.

Exercise 3 Write the template process-wagon for a function that processes a Wagon, and the template process-company for a function that processes a Company. Here’s a start:
(define (process-wagon w)
  (cond [(passenger-wagon? w)
         (... FILL IN THIS BLANK)]
        [(freight-wagon? w)
         (... FILL IN THIS BLANK)]))
 
(define (process-company c)
  (cond [(FILL IN THIS BLANK)
         ...]
        [(FILL IN THIS BLANK)
         ...]))
Each case of the template process-wagon should access all the fields of the input structure w, using courtesy functions you listed in Exercise 2.

Exercise 4 Point out where the data definition for Wagon refers to the data definition for Company. Does your template process-wagon refer to your template process-company in the corresponding place? It should.

Exercise 5 Design a function weight, which computes how many tons a Wagon weighs. An Alstom passenger wagon weighs 45 tons, and a Bombardier passenger wagon weighs 60 tons. Each axle of a freight wagon carries 6 tons of weight. Use the examples you defined in Exercise 1 in your tests, and follow the templates you wrote in Exercise 3. Those templates should guide you to design and use a helper function that computes the weight of a given passenger-wagon-model.

Exercise 6 Now we want to model a train of many wagons on a railroad track. A train might have no wagons (so it is just an engine), or it might contain a single wagon connected to the rest of the train. Develop a data definition and corresponding structure definitions for a TrainOfWagons. Here’s a start:
; A TrainOfWagons is one of:
; - (FILL-IN-THIS-BLANK)
; - (FILL-IN-THIS-BLANK Wagon TrainOfWagons)
Don’t use any existing structures other than passenger-wagon and freight-wagon above. Instead, define your own structures.

Exercise 7 Write three examples of TrainsOfWagons. Your three examples should make use of every line of your data definition.

Exercise 8 List the signatures for each of the courtesy functions for the structures you defined in Exercise 6.

Exercise 9 Write the template process-train-of-wagons for a function that processes a TrainOfWagons. Here’s a start:
(define (process-train-of-wagons t)
  (cond [(FILL IN THIS BLANK)
         (... FILL IN THIS BLANK)]
        [(FILL IN THIS BLANK)
         (... FILL IN THIS BLANK)]))
Like with process-wagon, each case of the template process-train-of-wagons should access all the fields of the input structure t, using courtesy functions you listed in Exercise 8.

Exercise 10 Point out where the data definition for TrainOfWagons refers to the data definition for Wagon. Does your template process-train-of-wagons refer to your template process-wagon in the corresponding place? It should.

Exercise 11 Point out where the data definition for TrainOfWagons refers to itself. Does your template process-train-of-wagons refer to itself in the corresponding place? It should.

Exercise 12 Design the function total-weight, which takes a TrainOfWagons as input and computes how many tons the whole train weighs. The engine weighs 130 tons. Use the examples you defined in Exercise 7 in your tests, and follow the template you wrote in Exercise 9. That template should guide you to define total-weight using the function weight you designed in Exercise 5 and the function total-weight itself.

2 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, larger circle at the new position, and the old circle should disappear.

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

Exercise 14 Write three examples of Worlds.

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

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

3 Challenges

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

Exercise 17 Modify the program from Exercise 16 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.

Exercise 18 Modify the program from Exercise 17 that instead of the circles staying where they start, they all move in some direction after being created.