CS 2135: Lab 2


Lab Motivation and Goals

This lab is designed to help you practice

Notes


Exercises

Consider the following data definitions for a music store inventory (copy and paste these from the browser into your DrScheme session):

  ;; A music-category is one of
  ;;   - 'classical
  ;;   - 'rock
  ;;   - 'blues

  ;; An album is a structure
  ;;  (make-album string string music-category)
  (define-struct album (artist title category))

  ;; A stock is a structure
  ;;  (make-stock album number number)
  (define-struct stock (album copies price))

  ;; An inventory is a list[stock]

(Note: As in other languages, you make strings in Scheme using double-quote, and strings can contain spaces and other whitespace characters. Example: "this is a string.")

In the following exercises, use map and filter where possible (but don't twist your code around to use them, only use them if they work naturally with the solution).

  1. Write the template for functions over inventories.

  2. Write a function total-in-stock that consumes an inventory and returns the total number of copies of all albums in the inventory.

  3. Write a function category-in-stock that consumes a category and an inventory and returns the total number of copies of all albums of that category that are in stock.

  4. Write a function time-to-order that consumes an inventory and produces a list of all album titles for which there are fewer than 2 copies in stock.

  5. Write a function blues-sale that consumes an inventory and returns an inventory with prices on all blues albums reduced 20%.

    Everybody should be able to finish up to this point

  6. Write a function affordable-by-artist that consumes an artist name (string), a maximum price (number), and an inventory and returns a list of all albums by that artist that are below the given price. You can compare strings using the operator string=?.

  7. Intermediate language level provides quicksort as a built-in operator. Here is its contract:

    ;; quicksort : (alpha alpha -> boolean) list-of-alpha -> list-of-alpha
    

    The first argument to quicksort is a function that compares two Scheme values and indicates when the first is less than the second. The second argument to sort is the list to sort. Note that the inputs to the ordering function (the first arg to sort) must be the same type as the elements of the list to sort (the second arg to sort).

    Using quicksort, write a function sort-by-price that consumes an inventory and returns an inventory. The returned inventory should be sorted in order of increasing price.

  8. Using quicksort, write a function sort-albums-by-type that consumes an inventory and returns an inventory. The returned inventory should contain the items from the input list, but with all the blues albums appearing at the front of the list, followed by the rock albums, followed by the classical. Within each category, the albums can appear in any order.

  9. Write sort-by-type-and-artist that consumes and returns an inventory. The returned inventory should contain the items from the input inventory sorted into categories as in the previous problem, but with the items sorted by artist name within each category. You can compare strings with the operators string (and its associated variants).