CS60A FALL 1993 Midterm 3 * Do not turn the page until instructed to do so. * This exam contains five numbered pages including the cover page. Put all answers on these pages, please; don't hand in stray pieces of paper. * This is an open book exam. * WE WILL NOT ANSWER ANY QUESTIONS ABOUT "INTERPRETATIONS" DURING THE EXAM. You may, however, ask if something is a typographical error. * This exam is worth 20 points, or 8% of your total course grade. The exam contains four substantive questions, plus the following: Question 0 (1 point): Fill out this front page correctly and put your name and login correctly at the top of each of the following pages. Question 1 (5 points): Your goal in this problem is to create two new classes of objects and make them work with the adventure game in your project. The two classes are the "BART station" class and the "BART ticket" class. A person is allowed to enter a BART station if and only if he/she owns a BART ticket. For this problem, let's assume that a BART ticket is more like a free pass - they don't cost any money, and no cost is associated with entering or leaving a BART station. However, entering a BART station without a ticket lands you in jail. 1. Define the BART station class as a subclass of the PLACE class. 2. Define the BART ticket class as a subclass of THING. 3. Describe changes to any other classes in the adventure game to make this work. I.e. What modifications are necessary to ensure that only people with BART tickets are allowed to enter a BART station? If you think no changes are necessary, then write that down. You may make any reasonable assumptions needed to clarify this question in your mind. Question 2 (4 points): Write a program (list-to-stream L) which takes a finite list L and returns a stream of its elements. That is, (list-to-stream '(1 2 3)) is a stream s with (head s) = 1 , (head (tail s))= 2 etc. and (tail(tail(tail s))) is the empty stream. Write a program (stream-to-list s n) which takes a stream s and returns a list of the first n elements from the stream. If the stream s is too short, return a list of as many elements as available. That is, (stream-to-list (list-to-stream '(1 2 3)) 2) is the list (1 2) . Here are some programs using streams (define (multstream st c) ;c is a number (if (empty-stream? st) st (cons-stream (* c (head st))(multstream (tail st) c)))) (define exp-stream (cons-stream 1 (multstream exp-stream 0.5))) (define (map-stream proc stream) ;; same as map on p 250 of text (if (empty-stream? stream) the-empty-stream (cons-stream (proc (head stream)) (map-stream proc (tail stream))))) * Redefine multstream but using map-stream: * What is returned for >(stream-to-list exp-stream 3) Question 3 (5 points): Our goal is to complete a procedure to make a slot machine (without using the OOP library) by filling in some blanks in a program. The procedure make-slot-machine takes as input the price needed to play and a jackpot value. It returns a procedure that takes messages to put-in-money and to pull-lever. It also takes a message from the casino-owner, under password control, to release all the money. When you pull the lever, you will also get change if you put in more than the necessary price. The code does not account for all the possibilities, such as too many winners. Usage: >(define m (make-slot-machine 50 500000 'xyzzy)) ;; need $0.50 to play m >((m 'put-in-money) 25) 25 >((m 'put-in-money) 25) 50 >((m 'pull-lever)) 0 ;;; or very rarely 500000 > ((m 'release-money) 'xyzzy) 500050 ;; unless the jackpot was won. Then $0. Fill in the lines as needed below (define (make-slot-machine price jackpot password) (let ((money-in 0) (total jackpot)) (define (put-in-money amount) (set! money-in (+ money-in amount)) money-in) (define (pull-lever) (if ( Question 4 (5 points): On the reverse of the previous page draw the environment diagram which results from executing the following code. BE NEAT! and be sure to include all relevant poin ters. (define (cons x y) (define (dispatch m) (cond ((=m 0) x) ((=m 1) y) (else (error "bad message")))) dispatch) (define (car z) (z 0)) (define k (cons 'a 'b)) Now we execute the following function call: > (car k) Show the additions you must make to the previous enxvironment diagram when this code is executed. Add pointers back to your first diagram where necessary.