On this page:
1 From unions to recursive unions
2 Extending food-truck Orders
8.6.0.14

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.

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)
Don’t use any existing structures. Instead, define your own structures.

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. 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!

Exercise 11. Write a new data definition for an Order that is a recursive union of structures. It should start like this:

; An Order is one of:

; - (make-??? ??? ???)

; - (make-??? ??? ??? Order ???)

(define-struct ??? ???)

(define-struct ??? ???)

It may help to introduce additional data definitions and use them in your definition of an Order. For instance, you might define what an Item is or what a Topping is, then include an Item or Topping in one of your Order structures.

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.

Exercise 16. Go visit another group and try out their menu:
  1. Read their data definition carefully, but don’t bother reading the rest of their code.

  2. Make an Order. Make sure to obey their data definition for what an Order is.

  3. Give your order to the draw-order function in their Interactions Window. Is it appetizing?

  4. 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.