This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: .__ : MC‘WeekIy uoxetine hydmdiloride NAME; ,3 SCORE LOGIN: m __ _ H mm 4 CS 611L_ ‘, Midterm #1 L. Rowe
Fall 2002 This examination is closed books, notes, and friends. Answer all questions in the space
provided. If you do not understand a question, please ask the proctor for clarification. Answer the questions using the Scheme functions discussed in class. You can use the
word/sentence abstraction but not mutators (e.g., set! , etc.), vectors, or continuations. If you do not remember the specific arguments to a function or the order of the arguments
to a function that takes several arguments, add a comment that describes how you are
using the function. We are interested in the comments, not details about function interfaces. Question Score Total Possible
1 (20 possible)
2 C R (10 possible)
slc.berkeley.edu
3 (51°) “2'73” (20 possible)
4 (10 possible)
5 (10 possible)
TOTA (70 possible) Oath
I certify that I am the student Signature: _
“m‘
Student ID #: g
Seating 3 E g I
E a "
On my left: Name; a E I A ' 5
S
On my right: Name:__ ‘ _ g2! é ., 5 [as F'02 CS 61A Midterm 1 of 6 pages 1. (20 points) Show what Scheme will display or do if you enter each expression to the
toplevel interpreter (i.e., at the STk> prompt). (i) (list ‘list) / t l 13“")
OD (car (car (list ‘a))) ﬁgﬁix v“\ I
/ Err Of :iv' = i/
ﬁﬁ) (first ‘(word 1 2 3)) I
J ' gwor‘d?
ﬁv) ((lambda () (/ 3 1))
(v) (define (f) (9))
(define (g) f)
(f) (vi) ((define (square x) I(* x x)) 2) .
/ ErrOr Ccodlﬁwa r/{e{"x\gaoat r) {or a) //(Vﬁ) ((lambda (X y) x) ‘(1 2)) V g. 1 a I junk (vﬁD (cons (cons 1 ‘()) (cons 2 ‘())) ../ l (C) ’3}! “ﬂ ‘ié‘Zl ( (fOO X Y) (£00 X ) WOrd \X \y) (X 4.) g
(X + y)) J 2 MW” lemUI mucu'iabw ‘ ?’ f  20% pages 2. (10 points) This question builds on the Twenty—one game from project 1. Instead of
representing a card by a word consisting of a number or letter and a letter for the
suit, we are going to use a pair. The pair will include a number or letter for the rank of
the card (i.e., 2, 3, ...,10, j, q, k, or a) and a letter for the suit (i.e., c, d, h, or s). For example the 4 of hearts and the king of diamonds will be represented as follows:
(cons 4 ' h)
(cons 'k 'd) (i) (2 points) Write a function makewcard that takes a rank and suit and returns a
pair representing the card. (MU(e (mitercclr& far“: fyeu‘i’) _‘
(Long; rcmiL lotil'l'3) ‘ @ (ii) (4 points) Write two accessor functions rank and suit. rank takes a card and
returns the number or letter of the card. suit takes a card and returns the suit.
For example: (define four_of_hearts (make—card 4 'h))
(define king_of__diamonds (make—card 'k 'd)) . (rank four_of_hearts) => 4
(rank king_of_diamonds) => k
(suit four_of__hearts) => h
(suit kingﬁof__diamonds) => (1
(olefiﬂe (rend; Card) (646(in (ew'i Card} (ca? Cm:f\3)_ 'LCOM’ (Jr.(r'c;\)j £9 Leak, fe+\,\“’\ seduce} 6‘096n’ircmA'tpmi' "Hterm (iii) (4 points) Write the function total using the new representation of a card.
total takes a “list of cards” and adds up their value assuming aces count as 11
and there are no jokers. ( ole{ E (if: (4, B I ( deﬂdc (“Mimi "he/I 3 LGT new.in
( o; (“MHZ 19+ ' ' Value: F'oacs 61A‘Midterm { {act/Wt? (m . . as. I ‘
(41%“ w l) 3C 0 ( + mut‘L—CCQJ‘ ‘31)} algae 010m,ch i' ', 5 law.” 3. (20 points) A word is a doublet of another word if they differ in only one letter. For
example, “noise” and “poise” are doublets, “poise” and “posse” are doublets, but .
“noise” and “posse” are not doublets because they differ in two letters. You are to write the procedure doublet? that takes two words and returns #t or #f indicating whether the two arguments are doublets. Doublet w d len . (Hint: you may need a helper function although the problem can be solved without
one.) (0“6‘0‘1, C divulging LNC} 2') I r
 . 7
( "g. "(moi (QCLtVLCA) 1’, (tenafh (length v.01. V) )> More (rammevmw, (ram M113 (we we) 2.) ))
' ( daublerhoheglé LiaGM! ) (bch‘L)waLie m ( dammit “creole ( be We 3er M1 7.) .‘ Comﬂ+33>> (deﬂﬁé {dﬁl‘U’H/‘l 1 Ul’Y’ln (C\$£""C UBﬂarl‘h "\ka C.c?v‘n“\' , ( 1L (eat/Mn? WC} CO pt 041' . Utensil)“.— he‘lpr \‘ Col/“.5415 ) ) ("'e“e+‘“ _ o a we 3). _  4 of 6 pages 3 4. (10 points) Draw arrows in the following code to demonstrate which variables refer
to which definitions”. For example: (define _( foo x) (+ 2)) shows that x in the body of the procedure is bound to the formal argument. Note that
there is no binding for the formal argument, so no arrow is drawn from it. (i) (5 points) Draw arrows to show the binding of each variable in the following
examples: ((lambda (x) (“Edi—55.)) (3))
_; PW \ (let ((x 3) (y 2)) _ 7 w \
(define (func ﬁ_hh('+.®(®—'§ ',€ree Lil m l i‘ﬁ‘bﬁfﬁ‘ USMC
(let ((x 100) (y 200)) I 9‘0"“ WW“ « (func’@ ) ) aroma. goof/i. g
wﬁN 7‘
I . dﬂ€l n H v.
(ii) (5 points) What does the let expressmn return? 6,. \w r L ,+ l / [OZ . (10 points) You are to write a procedure named agrees? that takes two arguments: 41541) a function E that takes one argument
2) a list of pairs in;
The “list of pairs” will be possible values of computing f. In other words, the first i
value of the pair will be an ac argument and the second value will be a possible
return result of the procedureghle agrees ? procedure will determine whether f
applied to the first value in eac pair produces a result that matches the second value
in the pair. You can think of this procedure as one the readers might construct to test a
homework assignment. The agrees ? procedure should return #t or #35 depending h ther all airs match. Exam leo next a e.
on»w e (dcginr (agfecg'z £11.55 p g ) J (ir— \17. {5+ ‘ai—t I
/ {larval (hour/Lat? ({— (Qmarfeﬂj (Cdaf 151)) (agrees7, «E (cdr (_s+\\\
Lﬁ‘t / ‘iiG))) PM 03 61A Midterm .. @ﬂ‘é
he: ’f'F'IfM M 0/: For example (agrees? (lambda (x) (* X x)) .
(list (cons 1 l) (cons 3 9))) => #t (agrees? (lambda (x) (* X x'))
(list (cons 2 4) (cons‘ l 5) (cons 6 36))) => #f (agrees? (lambda (x) (* x x)) ‘()) 2> #1: Write the function agrees ?: look, 0H’ (36.6" F'02 08 61A Midterm 6 of 6 pages ...
View
Full
Document
This test prep was uploaded on 04/20/2008 for the course CS 61A taught by Professor Harvey during the Fall '08 term at Berkeley.
 Fall '08
 Harvey
 Computer Science

Click to edit the document details