






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 05 – Midterm 2 Read and fill in this page now Your name: ___________________________________ Your instructional login (e.g., cs3-ab): ___________________________________ Your lab section days and time: ___________________________________ Your lab T.A.: ___________________________________ Name of the person sitting to your left: ___________________________________ Name of the person sitting to your right: ___________________________________ Prob 0: Prob 3: Prob 1: Prob 4: Prob 2: Prob 5: Raw Total : /50 Scaled Total : / You have 70 minutes to finish this test, which should be reasonable; there will be approximately 10 additional minutes of leeway given. Your exam should contain 6 problems (numbered 0-5) on 12 total pages. This is an open-book test. You may consult any books, notes, or other paper-based inanimate objects available to you. 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. Restrict yourself to scheme constructs that we have seen in this classs. (Basically, this excludes chapters 16 and up in Simply Scheme). Please write your answers in the spaces provided in the test; if you need to use the back of a page make sure to clearly tell us so on the front of the page. We believe we have provided more than enough space for your answers, so please don’t try to fill it all up. Partial credit will be awarded where we can, so do try to answer each question. Relax!
Problem (1 point) Put your login name on the top of each page. Also make sure you have provided the information requested on the first page. Problem (6 / 6 points). Remove-letter Consider a procedure remove-letter that takes two inputs, a letter and a sentence, and returns the sentence with all occurrences of the letter removed. For example: (remove-letter 'e '(here is a sentence with e in it) ^ (hr is a sntnc with "" in it) (remove-letter 'e '(not any within)) ^ (not any within) (remove-letter 'e '()) (^) () Part A: Write remove-letter without using any explicit recursion (i.e., use higher order functions instead)
Problem (10 points): Not just a ticky-tack question In tic-tac-toe, a pivot is an open square that identifies a winning move through the generation of a fork. In ttt.scm, the pivot procedure takes a sentence of triples and a player, and returns a sentence of pivots. The code in ttt.scm is reproduced in an appendix at the end of this exam. For the board b equal to "x o _ _ x _ _ _ o", for example: (pivots (find-triples b) 'x) (4 7) (pivots (find-triples b) 'o) (^) () Rewrite pivots without using higher order procedures (i.e., using only recursion). You can use procedures defined in ttt.scm as long as those procedures don't use higher order functions. (You may use appearances). Make sure to name your helper procedures and parameters well. You only need to comment when you think it necessary to help explain the intent of your procedure. Here are some procedures you can use without writing them: keep-my-singles takes a sentence of triples and a player and returns a sentence of triples that satisfy my-single? (that is, triples with two empty squares and one square filled by the player): (keep-my-singles (find-triples b) 'x) (^) ("4x6" x47 "3x7") (keep-my-singles (find-triples b) 'o) (^) ("78o" "36o") explode-all takes a sentence of words and returns a sentence with each word "exploded" into single-letter words: (explode-all '(bob joe)) (^) (b o b j o e) (explode-all '(25o 7o9)) (^) (2 5 o 7 o 9)
Problem continued (space for your answer)
Problem continued Part A (3 points):. This version is buggy because random is called several times instead of once per each of the n cycles—remember, each call to random may return a different value! Fix check-random-helper without defining any additional procedures. Part B (2 points). This version is buggy as well because, even with a correct fix to Part A , it always returns 0 as the minimum value of the set of random numbers. Fix this for all possible cases by modifying the check-random procedure (not the helper procedure) below:
Problem continued Part C (4 points). Write get-random-elements which takes a sentence and returns the first n elements of the sentence, where n is a random number. For instance: (get-random-elements '(this is a fine day)) can return either (), (this), (this is), (this is a), (this is a fine), or (this is a fine day) , Remember, (random n) returns an integer anywhere from 0 up to n-1. For instance, (random 5) can return either 0 , 1 , 2 , 3 , or 4. You may use either recursion or higher-order procedures. You may use helper procedures.
Problem (9 points): Does money grow on tree recursions? Consider a set of three coins: a penny, worth 1 cent; a nickle, worth 5 cents; and a dime, worth 10 cents. Write a procedure named possible-amounts which takes a number n, and returns a sentence of all the possible amounts that any n coins of these three types can make. For instance (possible-amounts 1) (^) (1 5 10) (possible-amounts 2) (^) (2 6 11 10 15 20) (This includes two pennies, a penny and a nickel, a penny and a dime, two nickels, a nickel and a dime, and two dimes) (possible-amounts 3) (^) (3 7 12 11 16 21 15 20 25 30) Fill in the blanks to make the definition of possible-amounts work correctly: (define coin-amounts __________________________) (define (possible-amounts n) (pa-helper coin-amounts n)) (define (pa-helper coins n) (cond ((<= n 1) ___________________) ;; base case 1 ((empty? coins) ________________________) ;; base case 2 (else (se (add-coin-to-every ;; recur case 1 (first coins) (pa-helper coins (- n 1))) (pa-helper ;; recur case 2
________________________________ ))))) ;; add coin to each element of sent (define (add-coin-to-every coin sent) (every (lambda (num) (+ coin num)) sent))
APPENDIX : the tic-tac-toe code from ttt.scm (define (ttt position me) (ttt-choose (find-triples position) me)) (define (find-triples position) (every (lambda (comb) (substitute-triple comb position)) '(123 456 789 147 258 369 159 357))) (define (substitute-triple combination position) (accumulate word (every (lambda (square) (substitute-letter square position)) combination) )) (define (substitute-letter square position) (if (equal? '_ (item square position)) square (item square position) )) (define (ttt-choose triples me) (cond ((i-can-win? triples me)) ((opponent-can-win? triples me)) ((i-can-fork? triples me)) ((i-can-advance? triples me)) (else (best-free-square triples)) )) (define (i-can-win? triples me) (choose-win (keep (lambda (triple) (my-pair? triple me)) triples))) (define (my-pair? triple me) (and (= (appearances me triple) 2) (= (appearances (opponent me) triple) 0))) (define (opponent letter) (if (equal? letter 'x) 'o 'x)) (define (choose-win winning-triples) (if (empty? winning-triples) #f (keep number? (first winning-triples)) )) (define (opponent-can-win? triples me) (i-can-win? triples (opponent me)) ) (define (i-can-fork? triples me) (first-if-any (pivots triples me)) ) (define (first-if-any sent) (if (empty? sent) #f (first sent) ))