8.14

### Lab 6: Recursive unionsðŸ”—

Last time in Lab 5: Unions, you designed a function wagon-weight that computes the weight of a given wagon. This design required the data definition of a Wagon and its corresponding template process-wagon. Now we want to model a train that might be made of any number of wagons.

Exercise 1. 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)

Exercise 2. Write three examples of TrainsOfWagons. Your three examples should make use of every line of your data definition. Hint: Use the example Wagons you defined last time.

Exercise 3. List the names of all the courtesy functions for the structures you defined in Exercise 1.

Exercise 4. 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 last time, 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 3.

Exercise 5. 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 last time in the corresponding place? It should.

Exercise 6. 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 7. Design the function train-length, which takes a TrainOfWagons as input and counts how many wagons it has. Use the examples you defined in Exercise 2 in your tests, and follow the template you wrote in Exercise 4.

Exercise 8. In the previous exercise, you should have written at least one function example where the input to train-length is a TrainOfWagons with some Wagons. Calculate step-by-step from this example.

But when you get to the point where train-length is recursively called, step directly to what the result of the call should be.

Exercise 9. Design the function train-weight, which takes a TrainOfWagons as input and computes how many tons the whole train weighs. As you might remember from last time, the engine weighs 130 tons. Again, use the examples you defined in Exercise 2 in your tests, and follow the template you wrote in Exercise 4. That template should guide you to define train-weight using the function wagon-weight you designed last time and the function train-weight itself.

Exercise 10. In the previous exercise, you should have written at least one function example where the input to train-weight is a TrainOfWagons with some Wagons. Calculate step-by-step from this example.

But when you get to the point where wagon-weight is called, step directly to what the result of the call should be. And when you get to the point where train-weight is recursively called, step directly to what the result of the call should be.

Exercise 11. Design the function has-passenger?, which takes a TrainOfWagons as input and determines whether it contains any passenger wagon.

Exercise 12. Design the function freight-only, which takes a TrainOfWagons as input and produces a possibly shorter TrainOfWagons by keeping only freight wagons. Again, use the examples you defined in Exercise 2 in your tests, and follow the template you wrote in Exercise 4.