CS61A, Spring 1994
Midterm #1
Question 1 (3 points):
What will Scheme print in response to the following expressions? Assume
that they are typed in sequence, so definitions affect later interactions.
If an expression produces an error message, you may just say ``error''; you
don't have to provide the exact text of the message. If the value of an
expression is a procedure, just say ``procedure''; you don't have to show
the form in which Scheme prints procedures.
(+ (* 3 4 0 7) 8)
(lambda (x) (/ x 0))
(butfirst '(help!))
((if 3 * +) 4 5)
(let ((+ -))
(+ 8 2))
(+ 9 3)
Question 2 (1 point):
One or more of the following procedures generates an iterative process.
Circle them. Don't circle the ones that generate a recursive process.
(define (butfirst-n num stuff)
(if (= num 0)
stuff
(butfirst-n (- num 1) (bf stuff))))
(define (member? thing stuff)
(cond ((empty? stuff) #f)
((equal? thing (first stuff)) #t)
(else (member? thing (bf stuff)))))
(define (addup nums)
(if (empty? nums)
0
(+ (first nums)
(addup (bf nums)))))
Question 3 (5 points):
Write a predicate procedure increasing? that takes as its
argument a sentence of numbers. It should return true if all the
numbers are in increasing order (not allowing equality), false otherwise:
> (increasing? '(4 17 25 32))
#t
> (increasing? '(4 17 3 32))
#f
> (increasing? '(4 17 17 32))
#f
> (increasing? '(4))
#t
Assume that the argument sentence will always contain at least one
number.
Question 4 (5 points):
A strategy procedure in the 21 project takes two arguments, the player's
hand so far and the dealer's visible card, as in this example:
(my-strategy '(3h 4d 10c) '5s)
It returns true to indicate that the player wants another card, false
otherwise.
(a) Write a strategy four-cards that always takes another card
if the player has fewer than four cards, but stops when the player
has four cards. (This isn't a very good strategy!) Reminder: you
can use count to count the number of words in a sentence.
(b) Write a procedure n-cards that takes a number n as its
argument, returning a strategy that stops when the player has n cards.
For example, (n-cards 4) should return a strategy equivalent to the
one you wrote in part (a).
Question 5 (5 points):
Some people from Brooklyn interchange the sounds of ``oi'' and ``er.''
For example, they pronounce ``fern'' as ``foin'' and ``foil'' as ``ferl.''
Write a procedure brooklyn that takes a word as its argument,
returning a word in which each oi is replaced by er and
each er is replaced by oi:
> (brooklyn 'mermaid)
MOIMAID
> (brooklyn 'hoi-polloi)
HER-POLLER
> (brooklyn 'salami)
SALAMI
Assume that the argument word will always contain at least one letter.
Take a peek at the
solutions