8.12

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

#### 1UnionsðŸ”—

Exercise 1. Define three examples of railroad Wagons, according to the following data and structure definitions:
 ; 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))
Your three examples should make use of every line of these data definitions.

Exercise 2. List the names of all the courtesy functions for the passenger-wagon and freight-wagon structures.

Exercise 3. Write the template process-wagon for a function that processes a Wagon, and the template process-company for a function that processes a Company. Here’s a start:
 (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) ...]))
Each case of the template process-wagon should access all the fields of the input structure w, using courtesy functions you listed in Exercise 2.

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.

Exercise 6. 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.

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.

#### 2Nested 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).

Exercise 7. Develop a data definition and corresponding structure definitions for a Shuttle. Here’s a start:
 ; A Shuttle is one of: ; - (FILL-IN-THIS-BLANK) ; - (FILL-IN-THIS-BLANK Wagon) ; - (FILL-IN-THIS-BLANK Wagon Wagon)
Don’t use any existing structures other than passenger-wagon and freight-wagon above. Instead, define your own structures.

Exercise 8. Write three examples of Shuttles. Your three examples should make use of every line of your data definition.

Exercise 9. List the names of all the courtesy functions for the structures you defined in Exercise 7.

Exercise 10. Write the template process-shuttle for a function that processes a Shuttle. Here’s a start:
 (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)]))
Like with process-wagon, each case of the template process-shuttle should access all the fields of the input structure s, using courtesy functions you listed in Exercise 9.

Exercise 11. 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 12. 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 8 in your tests, and follow the template process-shuttle you wrote in Exercise 10. That template should guide you to define shuttle-weight using the function wagon-weight you designed in Exercise 6.