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
; 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.
; process-wagon : Wagon -> ... (define (process-wagon w) (cond [(passenger-wagon? w) (... FILL IN THIS BLANK)] [(freight-wagon? w) (... FILL IN THIS BLANK)]))
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
; A TrainOfWagons is one of: ; - (FILL-IN-THIS-BLANK) ; - (FILL-IN-THIS-BLANK Wagon TrainOfWagons)
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.
; 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.