CS 3 (Clancy) Exam 2 April 6, 1992 Read and fill in this page now. Do NOT turn the page until you are told to do so. Your name: (please printÑlast name, first name) Your lab section day and time: Your lab t.a.: Name of the person sitting to your left: Name of the person sitting to your right: Problem 0 Total: /30 Problem 1 Problem 4 Problem 2 Problem 5 Problem 3 Problem 6 This is an open-book test. You have approximately fifty minutes to complete it. You may consult any books, notes, or other inanimate objects available to you. To avoid confusion, read the problems carefully. If you find it hard to understand a problem, ask us to explain it. If you have a question during the test, please come to the front or the side of the room to ask it. This exam comprises 15% of the points on which your final grade will be based. Partial credit will be given for wrong answers. You are not to use setf within functions. Any other Lisp construct described in Touretzky chapters 1-8, either part of the ÒDifference Between DatesÓ case study, the ÒStatisticsÓ case study, the supplementary reading on applicative operators, or ÒInteger Division and its UsesÓ is ok, though. Your exam should contain 7 problems (numbered 0 through 6) on 9 pages. Please write your answers in the spaces provided in the test; in particular, we will not grade anything on the back of an exam page unless we are clearly told on the front of the page to look there. RelaxÑthis exam is not worth having heart failure about. Problem 0 (2 points) Put your name on each page. Also make sure you have provided the information requested on the first page. Background for the exam problems Problems 1-6 all concern a function total-rainfall that, given a Òrain tableÓ of month-rainfall data and a list of month names, returns the total rainfall for months with entries in the rain table whose names are also in the month name list. For example, given the table ((january 60) (february 42) (march 36) (april 24) (may 16) (june 14) (july 7) (august 13) (september 20) (october 34)) and the month name list (january september may december) total-rainfall would return 60+20+16 = 96. Total-rainfallÕs rain table input need not contain entries for all months of the year. Total-rainfallÕs month name list input will contain a list of month names; not all of these months will necessarily have entries in the rain table, however. For problems that require you to provide a definition for total- rainfall, you may make any assumptions you wish concerning the result to be returned by total-rainfall if its month name list contains duplicate month names. Problem 1 (6 points) One approach to the design of total-rainfall is to filter irrelevant table elements from the table, then add up the corresponding rainfall entries in the filtered table. In diagram form, using the example above, this approach would do the following: The function below implements this approach. Fill in each of the blanks with an applicative operator to complete the function. ; Return the total rainfall for months named in month-name-list ; with entries in rain-table, a list of pairs of month-rainfall ; figures. (defun total-rainfall (rain-table month-name-list) (defun relevant? (pair) (member (first pair) month-name-list) ) ( #'+ ( #'second ( #'relevant? rain-table) ) ) ) Problem 2 (3 points) What does the function you wrote for problem 1 return for the following call? Show your work for full credit. (total-rainfall '((january 10) (february 13) (march 17)) '(march march february december) ) Problem 3 (6 points) Problem 3a Suppose that, immediately after starting Lisp, you typed the following into the Listener window, where , , and are your answers to problem 1. (defun relevant? (pair) (member (first pair) month-name-list) ) (defun total-rainfall (rain-table month-name-list) ( #'+ ( #'second ( #'relevant? rain-table) ) ) ) Suppose that you then typed (total-rainfall '((january 5) (may 10)) '(may)) into the Listener window. Describe briefly but completely how the Lisp interpreter would respond. Problem 3b Suppose that, immediately after typing as described in part 3a, you typed (setf month-name-list '(january)) (total-rainfall '((january 5) (may 10)) '(may)) into the Listener window. Describe briefly but completely how the Lisp interpreter would respond. Problem 3c Suppose again that, immediately after starting Lisp, you typed the following into the Listener window, where , , and are your answers to problem 1. (defun relevant? (pair month-name-list) (member (first pair) month-name-list) ) (defun total-rainfall (rain-table month-name-list) ( #'+ ( #'second ( #'relevant? rain-table) ) ) ) Suppose that you then typed (total-rainfall '((january 5) (may 10)) '(may)) into the Listener window. Describe briefly but completely how the Lisp interpreter would respond. Problem 4 (4 points) The first approach to the design of total-rainfall was to create a sublist of rain table entries and to transform it to the appropriate list of rainfall figures to add. Another approach creates a list of rainfall figures more directly by finding, for each month in month-name-list, the associated rainfall figure from the rain table; those values are then totalled as in the first approach. The function below implements the second approach. Complete the function by filling in the blank. You may supply auxiliary functions; indicate clearly whether they are defined inside or outside total-rainfall. None of the code you provide should use any applicative operators. ; Return the total rainfall for months named in month-name-list ; with entries in rain-table, a list of pairs of month-rainfall ; figures. (defun total-rainfall (rain-table month-name-list) (reduce #'+ (mapcar month-name-list)) ) Problem 5 (2 points) What does the function you wrote for problem 4 return for the following call? Show your work for full credit. (total-rainfall '((january 10) (february 13) (march 17)) '(march march february december) ) Problem 6 (7 points) Given below is a recursive implementation of total-rainfall, with some of the Lisp expressions replaced by , , , and . ; Return the total rainfall for months named in month-name-list ; with entries in rain-table, a list of pairs of month-rainfall ; figures. (defun total-rainfall (rain-table month-name-list) (cond ( 0) ((member month-name-list) (+ ) ) ) ( T ) ) ) Problem 6a Total-rainfallÕs base case is indicated by in the function framework above. Supply the base case, in Lisp, below. Problem 6b The recursive calls are indicated by in the function framework above. They are identical. Fill in the input expressions below. (total-rainfall ) Problem 6c Supply the first input to member, indicated by in the function framework above.