As of this lab, move to language level "Intermediate student with lambda" (under the "Language" menu in DrScheme).
Scheme has a built-in operator list
that you can
use to build lists. Instead of writing
(cons 1 (cons 7 (cons 5 empty)))
you can write (list 1 7 5)
. Note that the list
version does not contain the empty
. You can use
cons
or list
to write down lists for your
test cases (but don't use list
in place of
cons
inside your functions).
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).
Write the template for functions over inventories.
Write a function total-in-stock
that consumes an
inventory and returns the total number of copies of all albums in the
inventory.
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.
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.
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
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=?
.
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.
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.
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).