Due by 10:00am on Tuesday, 8/2

Instructions

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

Complete the quiz and submit it before 10:00am on Tuesday, 8/2. 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. Though we will not release these hidden tests to you, you will receive autograder feedback specifying whether or not you have passed all tests.

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:

Converting a cond form to if forms

As we saw in Lecture 20, Scheme source code is data. Every non-primitive expression is a list, and we can write procedures that manipulate other programs just as we write procedures that manipulate lists.

In Problem 19 of the project, you'll write a procedure let-to-lambda which transforms all let special forms within a piece of Scheme code into equivalent lambda procedure calls.

In this problem, we'll look at how we can convert a cond special form into an equivalent structure of nested if forms.

For example, the following cond form from the filter procedure:

(cond ((null? s) '())
      ((f (car s)) (cons (car s) (filter f (cdr s))))
      (else (filter f (cdr s))))

could instead be rewritten as a series of nested if forms:

(if (null? s)
    '()
    (if (f (car s))
        (cons (car s) (filter f (cdr s)))
        (filter f (cdr s))))

Note that the logic for evaluating both expressions is the same. You first evaluate (null? s) and if it's true, you return '(). If not, you then evaluate (f (car s)) and if it's true, you return (cons (car s) (filter f (cdr s))). If that's not true, then you've reached the else case and can just return (filter f (cdr s)).

Question 1

Before we convert an entire cond form, we'll create a few procedures to extract different parts of the form that we can use in our solution.

A cond form consists of a series of clauses, which are lists containing two items, a predicate and a consequent. First, the predicate is evaluated and, if it is true, the consequent is evaluated and returned. If not, the next clause's predicate is tried.

Note: While actual cond clauses can contain any number of consequent expressions, we'll assume for the purpose of this quiz that each clause contains exactly one consequent.

Implement the four helper procedures below. predicate and consequent both take in a single cond clause and return the predicate and consequent respectively. first-clause takes in a complete cond form and returns the first clause it contains. rest-clauses also takes in a cond form, but instead returns a list of all clauses but the first.

; Returns the predicate expression of a cond clause
(define (predicate clause)
  'YOUR-CODE-HERE
)

; Returns the consequent expression of a cond clause
(define (consequent clause)
  'YOUR-CODE-HERE
)

; Returns the first clause of a cond form
(define (first-clause expr)
  'YOUR-CODE-HERE
)

; Returns all clauses but the first of a cond form
(define (rest-clauses expr)
  'YOUR-CODE-HERE
)

Use OK to unlock and test your code:

python3 ok -q helpers -u
python3 ok -q helpers

Question 2

We can now use our helper procedures as part of our solution for cond-to-if, which takes in a single cond expression and converts it to a nested structure of if forms.

Note that the nested structure will display entirely on one line, since there is no way to automatically display it nicely. This is important for unlocking the tests.

At each level of recursion, you should construct an if form based on the first clause.

You should first handle the base case where the current clause's predicate is the symbol else, in which case you can just return the consequent, as a cond such as (cond (else 4)) is just equivalent to 4.

If the current predicate is not else, you should construct an if of the form (if <predicate> <consequent> <alternative>) where the alternative should be constructed through a recursive call.

Implement cond-to-if, which does this conversion. You may assume that expr consists of a single valid cond form with no other cond forms nested inside of it and that it will always end with an else clause.

Hint: You can check if two symbols are equivalent with the equal? predicate.

scm> (equal? 'hi 'bye)
False
scm> (equal? 'bye 'bye)
True
; Takes in a single cond form and returns an equivalent nested structure of if
; forms. You may assume that there are no nested cond forms inside of the main
; one, that each clause has a single consequent, and that the last clause is an
; else clause.
(define (cond-to-if expr)
  'YOUR-CODE-HERE
)

Use OK to unlock and test your code:

python3 ok -q cond-to-if -u
python3 ok -q cond-to-if