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
Define a data type
CafeDrinkwith a single constructordrinkthat has three fields:name(a string),size-oz(a positive number, the size in ounces) andprice(a positive number, in dollars). Hint 1 and hint 2.Define the names
latte,teaandespressoto be the following drinks:- name “latte”, 12 ounces, $4.50
- name “tea”, 16 ounces, $3.20
- name “espresso”, 2 ounces, $2.50
Define a function
price-per-oz(along with a check-block namedcheck-price-per-oz) that takes a drink and returns its price per ounce. For example,price-per-oz(tea)is0.2.Click here for a hint
You will need to extract fields.
Define a function
total-price(along with a check-block namedcheck-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.
Define a function
with-discount(along with a check-block namedcheck-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)isdrink("latte", 12, 3.6).
Check your understanding: what is the price of the drink stored inlatteafter you callwith-discount(latte, 20)and why? where does the discounted price go?
Part 2: Points on the plane
Define a data type
Point2Dwith a single constructorpoint2dthat has two fields,xandy(both numbers). Then define the namesoriginandpt-ato be the points (0, 0) and (-3, 4).Define a function
is-origin(along with a check-block namedcheck-is-origin) that takes a point and returnstrueif it is the origin, andfalseotherwise.
Define a function
distancethat 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))is5.Define a data type
Quadrantwith five variants and no fields:Q1,Q2,Q3,Q4for the four quadrants, andAxisfor a point that lies on the x-axis or the y-axis. Hint.Define a function
quadrant(along with a check-block namedcheck-quadrant) that takes a point and returns its quadrant. For example,quadrant(pt-a)isQ2.
Dig Deeper
Define a function
my-mapwith two type variables,FandT(for “from” and “to”), that takes a functionfn(which takes a value of typeFand returns a value of typeT) and a list ofFs, and returns a list ofTs, where each value in the output list is the result of applyingfnto the corresponding value in the input list. Hint.Using
my-map, find the list of lengths of strings in the list[list: "a", "cat", "isn't", "", "a", "dog"].Define the name
drinks-menuto be the set oflatte,tea,espresso, and a drink called “latte large” that is 20 ounces and $6.Define a function
set-total-pricethat 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.