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 wagon-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.
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 7. Write three examples of Shuttles. Your three examples should make use of every line of your data definition.
Exercise 8. List the names of all the courtesy functions for the structures you defined in Exercise 6.
(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 10. 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 11. 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 7 in your tests, and follow the template you wrote in Exercise 9. That template should guide you to define shuttle-weight using the function wagon-weight you designed in Exercise 5.
3 Union as food-truck Order
Do this section in groups of at least 2 people.
Remember your food truck in Lab 2: The design recipe, Lab 3: Multiple cases, and Lab 4: Structures? To offer even more customization, you decided to change the menu again.
; An Order is one of: |
; - (make- |
; - (make- |
(define-struct |
(define-struct |
Exercise 13. Define at least 3 examples of Orders. Define as many examples as it takes to make use of every line of your data definition.
Exercise 14. List all the courtesy functions for the structures you just defined.
Exercise 15. Write the template for processing an Order.
Exercise 16. Feel free to do this exercise and the next exercise in parallel. However, it is very important that y’all use the same data definition for an Order that y’all just wrote.
Design a function price-order that takes an Order and returns its price (a number).
Exercise 17. Design a function draw-order that takes an Order and returns a crude image of the food.
Read their data definition carefully, but don’t bother reading the rest of their code.
Make an Order. Make sure to obey their data definition for what an Order is.
Give your order to the draw-order function in their Interactions Window. Is it appetizing?
Give the same order to the price-order function in their Interactions Window. Is it worth it?
Exercise 19. (Challenge) Instead of requiring your customers to enter their Orders by following the technical instructions in your data definition, create a big-bang program that allows a customer to edit an Order interactively and graphically. To create such a program, you need to design on-key and/or on-mouse handler functions that switch among the possible kinds of Orders and that adjust the fields of the structure. For instance, you might let the user press a key to switch between ordering a pizza and ordering a burger, and drag the mouse to adjust the size of the pizza or burger.