Lab 6: Recursive unions
1 From unions to 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.
; 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.
(define (process-train-of-wagons t) (cond [(FILL IN THIS BLANK) (... FILL IN THIS BLANK)] [(FILL IN THIS BLANK) (... FILL IN THIS BLANK)]))
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. 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 9. Design the function has-passenger?, which takes a TrainOfWagons as input and determines whether it contains any passenger wagon.
Exercise 10. 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.
2 Extending food-truck Orders
Now apply the lessons above to your food-truck program from last time. Do this section in groups of at least 2 people.
Now that you have streamlined the production process on your food truck, you are finally ready to grow your business and let customers place orders of unlimited size!
; An Order is one of: |
; - (make-??? ??? ???) |
; - (make-??? ??? ??? Order ???) |
(define-struct ??? ???) |
(define-struct ??? ???) |
Exercise 12. Define at least 3 examples of Orders. Define as many examples as it takes to make use of every line of your data definition(s).
Exercise 13. Write the template for processing an Order. If you introduced other data definitions, write the templates for processing them as well.
Point out each place where a data definition refers to itself or another data definition. Does your template refer to itself or another template in the corresponding place? It should.
Exercise 14. Feel free to do this exercise and the next exercise in parallel. However, it is very important that y’all use the same data definitions that y’all just wrote.
Design a function price-order that takes an Order and returns its price (a number).
Exercise 15. 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 17. (Challenge) Like in the challenge exercise in Lab 5: Unions, create a big-bang program that allows a customer to edit an Order interactively and graphically. For instance, you might let the user press a key to add or remove a topping, and drag the mouse to adjust the amount of the topping.