Lab 5: Unions
Note: Whenever you design or write a function, you need to follow the design recipe.
Recall unions of structures from Lecture 10: Unions of structures.
1 Unions
; 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))
Exercise 2. List the names of all the courtesy functions for the passenger-wagon and freight-wagon structures.
(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) ...]))
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 company-weight, which takes a Company as input and computes how many tons its passenger wagon model weighs. An Alstom passenger wagon weighs 45 tons, and a Bombardier passenger wagon weighs 60 tons. Follow the template process-company you wrote in Exercise 3.
(company-weight "Bombardier") = ???
Exercise 7. Design a function wagon-weight, which takes a Wagon as input and computes how many tons it weighs. 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 template process-wagon you wrote in Exercise 3. That template should guide you to define wagon-weight using the function company-weight you designed in Exercise 5.
(wagon-weight (make-passenger-wagon "Bombardier")) = ???
Save your lab work for use next week! If you submit it using Handin as assignment lab5, you will be able to retrieve it from anywhere.
2 Nested structures
Now we want to model a train of wagons on a railroad track. A shuttle is a train that that contains up to two wagons. In other words, a shuttle might have no wagons (so it is just an engine), or one wagon (attached to an engine), or two wagons (attached to an engine).
; A Shuttle is one of: ; - (FILL-IN-THIS-BLANK) ; - (FILL-IN-THIS-BLANK Wagon) ; - (FILL-IN-THIS-BLANK Wagon Wagon)
Exercise 10. Write three examples of Shuttles. Your three examples should make use of every line of your data definition.
Exercise 11. List the names of all the courtesy functions for the structures you defined in Exercise 9.
(define (process-shuttle s) (cond [(FILL IN THIS BLANK) (... FILL IN THIS BLANK)] [(FILL IN THIS BLANK) (... FILL IN THIS BLANK)] [(FILL IN THIS BLANK) (... FILL IN THIS BLANK)]))
Exercise 13. Point out where the data definition for Shuttle refers to the data definition for Wagon. Does your template process-shuttle refer to your template process-wagon in the corresponding places? It should.
Exercise 14. Design the function shuttle-weight, which takes a Shuttle as input and computes how many tons the whole shuttle weighs. The engine weighs 130 tons. Use the examples you defined in Exercise 10 in your tests, and follow the template process-shuttle you wrote in Exercise 12. That template should guide you to define shuttle-weight using the function wagon-weight you designed in Exercise 7.
Exercise 15. In the previous exercise, you should have written at least one function example where the input to shuttle-weight is a Shuttle with two Wagons. Calculate step-by-step from this example.
But when you get to the points where wagon-weight is called, step directly to what the result of the call should be.