



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS3 Fall 04 – Midterm 1 Solutions and Grading standards Problem 1 (6 points, 10 minutes): Make the expressions correct Add parentheses and procedures in the underlined areas to make these expressions return the value specified. Do not define any new procedures. Be careful with parentheses and quotes! You can leave the underlined areas blank, but all the explicit text must be included in your expression—you can't just leave something out. If it is impossible to create an expression that returns the value specified, write IMPOSSIBLE somewhere (to the right of the return value, say). Remember, this question was mis-worded: you could also add quoted words and sentences in the blanks below. This allowed for a solution to [4] below. 1] ( bl (bl (se 'scheme 'is 'a 'state 'of 'mind ) ) ) (scheme is a state) 2] (sentence a (item 3 '(state of mind) )) (a mind) 3] ( item 8 (first '(expression) ) ) i 4] ( word (first '(go cal bears) ) 'cal 'bears ) gocalbears 5] (count "" ) 0 6] (if (or #f (and _________________ #f)) 'yep 'nope) yep IMPOSSIBLE 7] (define (what-the sent) (if (empty? sent) "" (word "-um-" (first sent) (what-the (bf sent))))) (what-the '( I like "" cheese ) ) "-um-i-um-like-um--um-cheese" Part [5] also works with '(), which many of you used. Some of you came up with other solutions for part [7] that none of us thought of, e.g.: (what the '(I like –um-cheese)) Each of these problems was worth one point, but only six points were possible. Each problem was graded all-or-nothing (i.e., no partial credit).
Problem 2 (5 points, 10 minutes) : Validating dates for day-span Write a procedure called day-span-validated which takes the same arguments as day-span and returns the same value as day-span when the argument dates are valid (i.e., legal). If either of the dates is invalid, however, day-span-validated should return (invalid date). For instance: (day-span-validated '(february 30) '(june 3)) ^ (invalid date) (day-span-validated '(june 4) '(may 15)) ^
(day-span-validated '(january 19) '(flugalmonth 5)) (invalid date) (day-span-validated '(january three) '(may five)) (invalid date) You can assume that each argument to day-span-validated is a sentence containing two words. You can (and should) use procedures from the “Difference Between Dates” case study, part I, that will help you. This code is included in an appendix at the end of the exam. In particular, consider the procedures day-span, days-in-month, month-name, and date-in-month. Assume that this is not a leap year. There were three basic things you had to do for this problem:
Problem 3 (6 points, 10 minutes): Data Abstraction in School You are part of a team working on a project to create, among other things, a database system for schools. One of your tasks is to implement the school datatype in Scheme. This question does not require recursion. In the project, a school will have three pieces of information: A short name, which will be a single word, A full name, which will be one or more words, Enrollment, a number For instance, here are some possible schools : full name short name enrollment University of California Berkeley Cal 23000 Rice Rice 2822 California Institute of Technology Caltech 900 This problem confused many people, but you have seen constructors and selectors in lab, lecture, and the book (look it up!). The Scheme syntax of the solutions was hardly difficult, which may have thrown some of you. The concept of data abstraction is a difficult one, but something important for this course! Part A Make a constructor procedure for the school datatype. Choose a reasonable name for the procedure, and include good comments. (You will, of course, need to decide how the data is stored; there is more than one correct way to do this.) Include a call to your constructor. A solution: (define (make-school fname sname enroll) (se sname enroll fname)) (make-school '(university of california Berkeley) 'cal 23000) (cal 23000 university of california berkeley Other orderings of arguments and packaging up of the sentence were possible. Packaging up the three pieces of information into a word, while technically possible, would have required recursion in the selectors. You lost 0.5 points for forgetting to provide a call; 0.5 points for a poor name. Bad or missing comments lost no points. Using selectors or including wrong arguments lost between 1 and 2 points, depending on the rest of the logic. Part B Make the three selector (accessor) procedures, with reasonable names and comments. DO NOT use recursion when writing these.
Using the above constructor: (define (full-name sch) (bf (bf sch)) ) (define (short-name sch) (first sch) ) (define (enrollment sch) (first (bf sch)) ) Not writing selectors consistent with your constructor lost 0.5 points each (or 1 point from the constructor, depending). Other problems were worth between 0.5 to 1 point for each selector. Part C What is a reasonable name of the boolean test procedure for this datatype? (That is, the procedure that somebody can use to test whether a Scheme value is a school or not.) Don't write this procedure, just name it. The best name is school? , as with other predicates. Not having the question mark lost 1 point; a name that didn't imply "is this a school" lost 1 point [the majority of you got this question]. Problem 4 (5 points, 9 minutes): Argue Write a function called argue-response which takes a sentence and returns a sentence in which the meaning is reversed. Perhaps the easiest way to explain how it reverses meaning is through some examples: (argue-response '(I hate the opera)) (^) (you love the opera) (argue-response '(you love cookies)) (^) (I hate cookies) (argue-response '(you hate everything that I cook)) ^ (I love everything that you cook) If the first two words in the sentence are not of the form you've seen, argue- response takes a different tack, adding (all you ever say is) to the input: (argue-response '(lets have dinner)) (^) (all you ever say is lets have dinner) (argue-response '(what)) (^) (all you ever say is what) The third example in the first set of test cases above contained an unfortunate error: the second to last "I" in the argument was transformed to a "you" by argue-response. Only a few of you caught this: solving for it required recursion. You were not taken off for not doing this recursion; including a recursive solution gave you some leeway to miss a point elsewhere (which no one did).
Problem 5 (5 points, 10 minutes): Count-vowels Write a recursive procedure named count-vowels which takes a single word as input, and returns the number of vowels in the word. Assume that the function vowel? has been defined. You may define and use any other helper functions that make sense to you. For instance, (count-vowels 'fred) (^) 1 (count-vowels 'frederick) (^) 3 (count-vowels 'happy) (^) 1 This question seemed reasonable for most of you: a recursion down a word in which the recursive case conditionally added 1 to the recursive call, and the base case was a number. A solution with ifs: (define (count-vowels wd) (if (empty? wd) 0 (if (vowel? (first wd)) (+ 1 (count-vowels (bf wd))) (count-vowels (bf wd))) )) The correct base case was worth 1 point total: 0.5 for getting the test right, 0.5 for getting the return value right. Handling each side of the condition in the recursive case (i.e., when (vowel? wd) returned true or false) was worth 2 points.