Due by 11:59pm on Friday, 11/6


Download quiz03.zip. Inside the archive, you will find a file called quiz03.scm, along with a copy of the OK autograder.

Complete the quiz and submit it before 11:59pm on Friday, 11/6. You must work alone, but you may talk to the course staff (see Asking Questions below). You may use any course materials, including an interpreter, course videos, slides, and readings. Please do not discuss these specific questions with your classmates, and do not scour the web for answers or post your answers online.

Your submission will be graded automatically for correctness. Your implementations do not need to be efficient, as long as they are correct. We will apply additional correctness tests as well as the ones provided. Passing these tests does not guarantee a perfect score.

Asking Questions: If you believe you need clarification on a question, make a private post on Piazza. Please do not post publicly about the quiz contents. If the staff discovers a problem with the quiz or needs to clarify a question, we will email the class via Piazza. You can also come to office hours to ask questions about the quiz or any other course material, but no answers or hints will be provided in office hours.

Submission: When you are done, submit with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be scored.

Using OK

The ok program helps you test your code and track your progress. The first time you run the autograder, you will be asked to log in with your @berkeley.edu account using your web browser. Please do so. Each time you run ok, it will back up your work and progress on our servers. You can run all the doctests with the following command:

python3 ok

To test a specific question, use the -q option with the name of the function:

python3 ok -q <function>

By default, only tests that fail will appear. If you want to see how you did on all tests, you can use the -v option:

python3 ok -v

If you do not want to send your progress to our server or you have any problems logging in, add the --local flag to block all communication:

python3 ok --local

When you are ready to submit, run ok with the --submit option:

python3 ok --submit

Readings: You might find the following references useful:

Warm-Up Question (Optional; 0 points)

Implement all, which takes a predicate procedure f and a list s. It returns whether (f x) returns true for all elements x in s. Hint: The filter procedure from lab has a similar structure.

(define (all f s)

Use OK to unlock and test your code:

python3 ok -q all -u
python3 ok -q all

Question 1

Implement every, which takes a two-argument procedure g and a list s. It returns a new list of every element x in s for which (g x y) returns True for every y in s (including itself). Hint: You can use the filter procedure that you defined in Lab. You can also use the all procedure from Question 0.

(define (every g s)

Use OK to unlock and test your code:

python3 ok -q every -u
python3 ok -q every


Cucumber is a card game. In our implementation, cards are positive integers (i.e. there are no suits), and there may be more than one of the same card in play.

Play proceeds in a series of rounds, called tricks. In a trick, each player, in turn, plays a card. On your turn, you must either take control by playing a card as high or higher than the highest card played so far or play the lowest card in your hand. Whoever last took control wins the trick and can start the next trick with any card.

The goal of the game is not to win the last trick.

Question 2

One Cucumber strategy is the fimping strategy. On our turn, we play the lowest card in our hand that can take control; if we have no such card, we play the lowest card in our hand.

Implement fimp, which takes a non-empty list of cards (in any order) called hand and the highest card played so far. The fimp procedure returns the card we should play under the fimping strategy.

(define (fimp hand highest)

Use OK to unlock and test your code:

python3 ok -q fimp -u
python3 ok -q fimp

Question 3

Write a legal procedure that takes a list of cards (in any order) called hand and the highest card played so far. It returns a list of pairs. The first element of each pair is a card that can be legally played. The second element is True or False and indicates whether or not this card takes control. The cards must be returned in the order that they appear in hand.

(define (legal hand highest)
  (define least (min hand))
  (define (result hand)
    (if (null? hand) nil (begin
        (define card (car hand))
  (result hand))

Use OK to unlock and test your code:

python3 ok -q legal -u
python3 ok -q legal

Here are some examples:

scm> (legal '(5 2 4 3) 4)
((5 . True) (2 . False) (4 . True))
scm> (legal '(4 2 4 3) 4)
((4 . True) (2 . False) (4 . True))
scm> (legal '(5 2 4 4 3) 5)
((5 . True) (2 . False))
scm> (legal '(5 2 4 4 3) 6)
((2 . False))
scm> (legal '(5 4) 3)
((5 . True) (4 . True))