CSci 1301: Problem Set 6

Due: Wednesday, October 20 at 11:59pm by e-mail

As always, please include a contract, a purpose, examples, and tests for each function.

Problem 1 (6 points)

Exercise 9.5.4 Note that the exercise also asks you to generalize the function so that it takes three parameters: a list of temperatures, a lower bound, and an upper bound. Assume that the interval is inclusive, i.e. the bounds represent acceptable temperatures. For instance:

(check-expect (check-range? (list 36.6 0.0 75) 5 95) false)
(check-expect (check-range? (list 36.6 0.0 75) 0 95) true)

Problem 2 (3 points)

Write a function double that takes a list of numbers and produces a new list in which every number has been multiplied by 2. For instance:

(check-expect (double (list 2 3 7)) (list 4 6 14))

Problem 3 (6 points)

Write a function sum-lists that takes two lists of numbers and creates a new list in which the first element is the sum of the first elements of the two lists, the second element is the sum of the two second elements, etc. If one list is shorter than the other then the rest of the elements in the longer list just appear by themselves at the end of the resulting list. For instance:

(check-expect  (sum-lists (list 1 2) (list 3 4)) (list 4 6))
(check-expect  (sum-lists (list 2 3) (list 5 6 7)) (list 7 9 7))
(check-expect  (sum-lists empty (list 5)) (list 5))

Problem 4 (8 points)

Exercise 10.2.4. A phone record is a structure that contains a name (a symbol) and a phone number (a 7-digit integer, for simplicity assuming the same area code for everyone). A phone directory is a list of phone records.
If a phone or a name is not in a phone directory, a symbol 'NotFound is returned.

Below is an example of a phone directory and the use of the two functions:

(define phone-directory (list
                         (make-phone-entry 'SallySmith 3456789)
                         (make-phone-entry 'BobAnderson 1234567)

(check-expect  (whose-number 3456789 phone-directory) 'SallySmith)
(check-expect  (whose-number 4444222 phone-directory) 'NotFound)
(check-expect  (phone-number 'SallySmith phone-directory) 3456789)
(check-expect  (phone-number 'MarySmith phone-directory) 'NotFound)

CSci 1301 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.