As of this lab, move to language level "Intermediate student with lambda" (under the "Language" menu in DrScheme).
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:
(cons empty empty)
(list empty empty)
(append empty empty)
(cons (list 1 2 3) (list 4 5))
(list (list 1 2 3) (list 4 5))
(append (list 1 2 3) (list 4 5))
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). Remember that you can also use map and
filter in conjunction with other functions on lists, such as the
built-in function length
(computes the length of a list).
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 : 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.
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).