CS 1102: Lab 2


Lab Motivation and Goals

This lab is designed to help you practice

Notes

As of this lab, move to language level "Intermediate student with lambda" (under the "Language" menu in DrScheme).


Warm up: the various list operators

You've now seen three operators for building lists: cons, list, and append. Their contracts are as follows:

For each of the following expressions, try to predict (a) what they will return and (b) what the length of the resulting list is, then check your answers in DrScheme:

  1. (cons empty empty)
  2. (list empty empty)
  3. (append empty empty)
  4. (cons (list 1 2 3) (list 4 5))
  5. (list (list 1 2 3) (list 4 5))
  6. (append (list 1 2 3) (list 4 5))

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 filter where possible (don't twist code around unnecessarily to do this, just use it where it is natural). Remember that you can also use filter in conjunction with other functions on lists, such as the built-in function length (computes the length of a list).

  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 : list-of-alpha (alpha alpha -> boolean) -> list-of-alpha
    

    The first argument to quicksort is the list to sort and the second is a function that compares two Scheme values and returns true if the first value should appear before the second in the sorted list. 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).