Answers to sample midterm 2 1. What happens when you omit the (empty? number-sent) clause in roman-sum. Answer is b; the resulting code crashes when the Roman numeral represented by number-sent ends in a prefix-prefixee pair, but returns the correct answer when the argument ends with an unprefixed number. For example, consider what happens with the call (roman-sum '(1 5)). The top-level call is handled by the (starts-with-prefix? number-sent) clause, and it results in a recursive call to roman-sum with an empty sentence (bf (bf number-sent)) as argument. Since the clause to handle an empty argument has been removed, roman-num crashes when it tries to evaluate (bf number-sent) in the only remaining base case. 2a. Fill in the blanks in the dupls-removed procedure. Solution 1: DonŐt add a duplicate to so-far. First and third blanks = (bf sent) Second blank = so-far Fourth blank = (sentence so-far (first sent)) Solution 2: Take duplicates out of (bf sent). First blank = (sentence (first sent) (remove (first sent) (bf sent)) Second blank = so-far or First blank = (remove (first sent) (bf sent)) Second blank = (sentence so-far (first sent)) 2b. Use your code to evaluate (dupls-removed '(A B C B)). Using solution 1, you get (A C B); using solution 2, you get (A B C). 3. Identify the type of value returned by f1 and f2. Answers: f1 returns a sentence of players (x's or o's). f2 returns the number of a square (1, 2, ..., 9) or #f. 4. Identify values for which the given implementation of roman-sum doesn't work correctly. Answer: Note that accumulate works as follows. For an accumulation procedure p, (accumulate p sent) produces the sequence of calls (p (first sent) (p (second sent) (p (third sent) ... (p (next-to-last sent) (last sent))))) Examining the given code, we see that the so-far value used to check for a prefix isn't the next digit but the whole value accumulated so far. For example, a call to roman-sum with the number sentence corresponding to cxliv produces the calls (+ (- (+ (- 5 1) 50) 10) 100) The computation produces the right answer for the wrong reason. When is the answer wrong? Any value correctly regarded as a prefix will also be regarded as a prefix with this code (if the prefix is less than the next digit, it will certainly be less than the whole accumulated sum of digits). An incorrect result will occur for Roman digits that should *not* be treated as prefixes, because they are equal to the immediately following digit. An example is the handling of xxx: (- (+ 10 10) 10) The leftmost x is treated as a prefix. (The second x would also have been treated as a prefix if the test had been (<= n so-far).) Thus the set of Roman numerals for which the given code produces the wrong answer is those Roman numerals that end with a Roman digit repeated three times or that contain a Roman digit repeated twice before the end of the Roman numeral. 5. Fill in the blanks in the ranks procedure. Answer: (define (ranks hand suit) (every butfirst (keep (lambda (card) (equal? (first card) suit)) hand) ) )