CSci 1301: Problem Set 6

Due: Wednesday, October 21 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.3 in Section 9.5. Note that the last line asks you to generalize the function. The generalized function should work as follows:


(price-below-threshold? (cons .75 (cons 1.95 (cons .25 empty))) 1.5)
;; expected value
false

(price-below-threshold? (cons .75 (cons 1.95 (cons .25 empty))) 2.0)
;; expected value
true

Problem 2 (6 points)

Exercise 9.5.4 in Section 9.5 Note that the exercise also asks you to generalize a function to a function that 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-range? (cons 36.6 (cons 0.0 (cons 75 empty))) 5 95)
;; expected value
false

(check-range? (cons 36.6 (cons 0.0 (cons 75 empty))) 0.0 95)
;; expected value
true

Problem 3 (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:


(double (list 2 3 7))
;; expected value
(list 4 6 14)

Problem 4 (5 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 zero is added to that element. For instance:


(sum-lists (list 1 2) (list 3 4))
;; expected value
(list 4 6)

(sum-lists (list 2 3) (list 5 6 7))
;; expected value
(list 7 9 7)

(sum-lists empty (list 5))
;; expected value
(list 5)

Problem 5 (8 points)

Exercise 10.2.4 in Section 10.2. A phone record is a structure that has 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.

Update 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)
                         ))

(whose-number 3456789 phone-directory)
;; expected value
'SallySmith

(whose-number 4444222 phone-directory)
;; expected value
'NotFound

(phone-number 'SallySmith phone-directory)
;; expected value
3456789

(phone-number 'MarySmith phone-directory)
;; expected value
'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.