On this page:
1 A union of structures
2 A recursive union

Lab 6: Recursive unions

You may submit this lab to lab6 on Handin (this is optional).

Important: Whenever you write a function in this class, follow the design recipe. You will be graded accordingly.

1 A union of structures

Exercise 1. Define three examples of railroad Wagons, according to the following data and structure definitions:
; A Wagon is one of:
;  - (make-passenger-wagon String)
;  - (make-freight-wagon String Number)
(define-struct passenger-wagon [model])
(define-struct freight-wagon [destination axles])

Some models of passenger wagons are Alstom and Bombardier. Your three examples should make use of every line of your data definition.

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 the data definition of Wagon. Here’s a start:
; process-wagon : Wagon -> ...
(define (process-wagon w)
  (cond [(passenger-wagon? w)
         (... FILL IN THIS BLANK)]
        [(freight-wagon? w)
         (... 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. Design a function wagon-weight, which computes how many tons a Wagon weighs. A 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.

2 A recursive union

Exercise 5. Now we want to model a train. 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 6. Write three examples of TrainsOfWagons. Your three examples should make use of every line of your data definition.

Exercise 7. List the signatures for each of the courtesy functions for the structures you defined in Exercise 5.

Exercise 8. Write the template process-train-of-wagons for the data definition of TrainOfWagons. Here’s a start:
; process-train-of-wagons : TrainOfWagons -> ...
(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 7. Because this template contains a Wagon, it should also contain an application of process-wagon.

Exercise 9. Point out where the data definition for TrainOfWagons refers to the data definition for Wagon. Point out where your template process-train-of-wagons refers to your template process-wagon in the corresponding place.

Exercise 10. Point out where the data definition for TrainOfWagons refers to itself. Point out where your template process-train-of-wagons refers to itself.

Exercise 11. Design the function train-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 6 in your tests, and follow the template you wrote in Exercise 8. That template should guide you to define train-weight using the function wagon-weight you designed in Exercise 4 and the function train-weight itself.