A8 — Introduction to structured data

Define and use your own data; take another look at built-in lists.

Due Oct 21, 2026

Estimated time to complete (just the assignment, excluding the prep materials): 60–80 minutes.

Prep materials: Week 8: Structured data.

What to do

Download the file a8.arr and add all your code there. Submit the file to Assignment 8 Submission on Brightspace. Remember to use the auto-grader, test your Brightspace submission, and send your score to Brightspace when you are done working on the submission.

As always, everything you need for the assignment is in the prep materials.

Essentials

Part 1: Drinks

  1. Define a data type CafeDrink with a single constructor drink that has three fields: name (a string), size-oz (a positive number, the size in ounces) and price (a positive number, in dollars). Hint 1 and hint 2.

  2. Define the names latte, tea and espresso to be the following drinks:

    • name “latte”, 12 ounces, $4.50
    • name “tea”, 16 ounces, $3.20
    • name “espresso”, 2 ounces, $2.50
  3. Define a function price-per-oz (along with a check-block named check-price-per-oz) that takes a drink and returns its price per ounce. For example, price-per-oz(tea) is 0.2.

    Click here for a hint

    You will need to extract fields.

  4. Define a function total-price (along with a check-block named check-total-price) that takes a list of drinks and returns the total price of all the drinks in the list.

    Click here for a hint

    You can use built-in list and math functions from Weeks 5 and 6 for this, or you can write a recursive function.

  5. Define a function with-discount (along with a check-block named check-with-discount) that takes a drink and a discount percentage, and returns a new drink with the same name and size but the discounted price. For example, with-discount(latte, 20) is drink("latte", 12, 3.6).
    Check your understanding: what is the price of the drink stored in latte after you call with-discount(latte, 20) and why? where does the discounted price go?

Part 2: Points on the plane

  1. Define a data type Point2D with a single constructor point2d that has two fields, x and y (both numbers). Then define the names origin and pt-a to be the points (0, 0) and (-3, 4).

  2. Define a function is-origin (along with a check-block named check-is-origin) that takes a point and returns true if it is the origin, and false otherwise.

  1. Define a function distance that takes two points and returns the distance between them, \(\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}\). For example, distance(pt-a, point2d(1, 7)) is 5.

  2. Define a data type Quadrant with five variants and no fields: Q1, Q2, Q3, Q4 for the four quadrants, and Axis for a point that lies on the x-axis or the y-axis. Hint.

  3. Define a function quadrant (along with a check-block named check-quadrant) that takes a point and returns its quadrant. For example, quadrant(pt-a) is Q2.

Dig Deeper

  1. Define a function my-map with two type variables, F and T (for “from” and “to”), that takes a function fn (which takes a value of type F and returns a value of type T) and a list of Fs, and returns a list of Ts, where each value in the output list is the result of applying fn to the corresponding value in the input list. Hint.

  2. Using my-map, find the list of lengths of strings in the list [list: "a", "cat", "isn't", "", "a", "dog"].

  3. Define the name drinks-menu to be the set of latte, tea, espresso, and a drink called “latte large” that is 20 ounces and $6.

  4. Define a function set-total-price that takes a set of drinks and returns the total price of all the drinks in the set.

Grading

Auto-graded for correctness and some aspects of style, with named check-blocks auto-graded for coverage.