A9 — Review, mutation, and for-loops

Revisit collective and structured data; start using variables that can change.

Due Oct 28, 2026

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

Prep materials: Week 9: Getting started with mutation and Python, in particular DCIC 9.1.8.5 for each loops in Pyret.

What to do

Download the file a9.arr and add all your code there. It already contains the games table from Assignment 3. Submit the file to Assignment 9 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.

Do not compute any answers by hand or type out lists that you could get from the table.

Essentials

Part 1: Review

  1. Define games-popular to be the table of games with at least 1800 copies sold. Hint.

  2. Define a data type Game with a single constructor game that has four fields: title (a string), platform (a string), rating (a number), and price (a non-negative number).

  3. Define a function is-highly-rated (along with a check-block named check-is-highly-rated) that takes a Game and returns true if its rating is at least 8.0, and false otherwise.

  4. Define a function row-to-game that takes a row of the games table and returns the corresponding Game. For example, row-to-game(games.row-n(1)) is game("Castle Run", "Switch", 7.8, 15). Hint.

  5. Define games-list to be the list of Games for all the rows of games, in the same order as in the table. Hint.

  6. Define games-list-highly-rated to be the list of Games in games-list that are highly rated. What is another way to get the same list without using games-list?

Required but not graded or submitted: create a dataclass Game in Python with the same fields as the Pyret game constructor, and define and test a function is_highly_rated.

Part 2: Variables that change and for each

  1. Define a mutable variable year-born with the value 1980. Hint.

  2. Define a function calc-age that takes a year and returns the age of someone born in year-born in that year. For example, calc-age(2026) is 46.

  3. After the last call to calc-age, update the value of year-born to 1990. What is the value of calc-age(2026) now? Why?

  4. Add the following function to your file:

    fun my-sqr-side-effect(n :: Number) -> Number block:
      year-born := year-born + 1
      n * n
    end

    Do you understand what it does? Why do we need to use block here? Hint.

  5. Compute the square of 3 by using my-sqr-side-effect and store the result in (immutable) my-sqr-3. What is the value of calc-age(2026) now? Why?

  6. How can we write tests for calc-age? Think about this and submit your proposal on Discord, to Testing calc-age in #questions, or react to an existing proposal if it matches your idea.

  7. Define a function total-price (along with a check-block named check-total-price) that takes a list of Games and returns their total price. For example, total-price(games-list) is 208. For this exercise, follow the template from DCIC.
    Think of at least two other ways to write this function.

  8. Define a function titles-under that takes a list of Games and a price, and returns the list of titles of the games that cost less than that price, in the same order as in the input list. For example, titles-under(games-list, 20) is [list: "Castle Run", "Farm Life"]. Once again, use for each to implement this function.

Dig Deeper

  1. Define a function loop-map with type variables F and T that works like my-map from Assignment 8, but uses for each. Check that loop-map(lam(g): g.title end, games-list) is the title column of games, and that loop-map works like my-map and built-in map for a few other examples.

  2. Define first-title to be the first game title of titles-under(games-list, 100) sorted alphabetically. Hint.

  3. Define a function similar that takes two strings and returns true if they are the same when ignoring case, and false otherwise. For example, similar("PC", "pc") is true, and similar("PC", "Switch") is false.

  4. Define a check-block check-first-title, check that first-title is both "battle arena" and "Battle ARENA" when ignoring case. Hint.

Grading

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