CS 61A: Homework 7

Due by 11:59pm on Wednesday, 4/8

Instructions

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

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:

Table of Contents

The Scheme interpreter is included in the starter ZIP archive. To run the Scheme interpreter, use the following command:

python3 scheme

To load a file (such as hw07.scm), use

python3 scheme -load hw07.scm

You can also use our online Scheme interpreter.

Question 1

Define the procedures cadr and caddr, which return the second and third elements of a list, respectively:

(define (cddr s)
  (cdr (cdr s)))

(define (cadr s)
  ; YOUR-CODE-HERE
  nil
)

(define (caddr s)
  ; YOUR-CODE-HERE
  nil
)

You can unlock and test using OK:

python3 ok -q cadr-caddr -u
python3 ok -q cadr-caddr

Conditional expressions

The cond special form is a general conditional expression, similar to a multi-clause conditional statement in Python. The general form of a conditional expression is:

(cond
    (<p1> <e1>)
    (<p2> <e2>)
    ...
    (<pn> <en>)
    (else <else-expression>))

consisting of the symbol cond followed by pairs of expressions (<p> <e>) called clauses. The first expression in each pair is a predicate: an expression whose value is interpreted as either true or false.

Conditional expressions are evaluated as follows. The predicate <p1> is evaluated first. If its value is false, then <p2> is evaluated. If <p2>'s value is also false, then <p3> is evaluated. This process continues until a predicate is found whose value is true, in which case the interpreter returns the value of the corresponding consequent expression <e> of the clause as the value of the conditional expression. If none of the <p>'s is found to be true, the interpreter returns the value of the else expression.

The word "predicate" is used for procedures that return true or false, as well as for expressions that evaluate to true or false.

Question 2

Using cond, define a procedure sign that returns -1 for negative arguments, 0 for zero, and 1 for positive arguments:

(define (sign x)
  ; YOUR-CODE-HERE
  nil
)

You can unlock and test using OK:

python3 ok -q sign -u
python3 ok -q sign

Question 3

Implement a procedure pow for raising the number b to the power of integer n that runs in Θ(log n) time. Hint: Use the built-in predicates even? and odd? and the square procedure to implement the procedure using successive squaring, as we did in lecture:

(define (square x) (* x x))

(define (pow b n)
  ; YOUR-CODE-HERE
  nil
)

You can unlock and test using OK:

python3 ok -q pow -u
python3 ok -q pow

Question 4

Implement a predicate ordered?, which takes a list of numbers and returns whether the numbers are in ascending order. A predicate is a procedure that returns true or false.

(define (ordered? s)
  ; YOUR-CODE-HERE
  nil
)

You can unlock and test using OK:

python3 ok -q ordered -u
python3 ok -q ordered

Question 5

Implement the procedure nodots, which takes a nested list of numbers that may not be well-formed and returns a nested list with the same content and structure, but which does not have any dots when displayed.

(define (nodots s)
  ; YOUR-CODE-HERE
  nil
)

You can unlock and test using OK:

python3 ok -q nodots -u
python3 ok -q nodots

Question 6

One way to represent a set is to store the elements in a sorted list that contains no repeated elements. Define a procedure contains? that returns whether a set s contains value v, assuming s is represented in this way. The Python implementation of this procedure is provided for your reference.

; Sets as sorted lists

(define (empty? s) (null? s))

(define (contains? s v)
    (cond ((empty? s) false)
          ; YOUR-CODE-HERE
          (else nil)
          ))

; Equivalent Python code, for your reference:
;
; def empty(s):
;     return len(s) == 0
;
; def contains(s, v):
;     if empty(s):
;         return False
;     elif s.first > v:
;         return False
;     elif s.first == v:
;         return True
;     else:
;         return contains(s.rest, v)

You can unlock and test using OK:

python3 ok -q contains -u
python3 ok -q contains

Question 7

Next, define add, which takes a set s and a value v as arguments. It returns a representation of a set containing the values in s and the value v. There should be no repeated elements in the return value.

(define (add s v)
    (cond ((empty? s) (list v))
          ; YOUR-CODE-HERE
          (else nil)
          ))

You can unlock and test using OK:

python3 ok -q add -u
python3 ok -q add

Question 8

Next, define intersect, which returns a set containing only values that appear in both sets s and t. Your implementation should run in linear time in the length of the input sets. The Python implementation of this procedure is provided for your reference.

Also, define union, which returns a set containing all values that appear in either set s or t.

(define (intersect s t)
    (cond ((or (empty? s) (empty? t)) nil)
          ; YOUR-CODE-HERE
          (else nil)
          ))

; Equivalent Python code, for your reference:
;
; def intersect(set1, set2):
;     if empty(set1) or empty(set2):
;         return Link.empty
;     else:
;         e1, e2 = set1.first, set2.first
;         if e1 == e2:
;             return Link(e1, intersect(set1.rest, set2.rest))
;         elif e1 < e2:
;             return intersect(set1.rest, set2)
;         elif e2 < e1:
;             return intersect(set1, set2.rest)

(define (union s t)
    (cond ((empty? s) t)
          ((empty? t) s)
          ; YOUR-CODE-HERE
          (else nil)
          ))

You can unlock and test using OK:

python3 ok -q intersect -u
python3 ok -q intersect

python3 ok -q union -u
python3 ok -q union

Question 9

Another way to represent a set is to store the elements in a binary search tree. A binary search tree's branches are either empty or binary search trees. Also, the entry at the root of the tree must be greater than all entries in the left branch and less than all entries in the right branch.

The following procedures define a data abstraction for a binary search tree.

; A data abstraction for binary trees where nil represents the empty tree
(define (tree entry left right) (list entry left right))
(define (entry t) (car t))
(define (left t) (cadr t))
(define (right t) (caddr t))
(define (empty? s) (null? s))
(define (leaf entry) (tree entry nil nil))

Define the procedure in?, which returns whether a binary search tree t contains an element v. Your implementation should run in logarithmic time in the number of entries in a balanced input tree. The Python implementation of this procedure is provided for your reference.

(define (in? t v)
    (cond ((empty? t) false)
          ; YOUR-CODE-HERE
          (else nil)
          ))

; Equivalent Python code, for your reference:
;
; def contains(s, v):
;     if s.is_empty:
;         return False
;     elif s.entry == v:
;         return True
;     elif s.entry < v:
;         return contains(s.right, v)
;     elif s.entry > v:
;         return contains(s.left, v)

You can unlock and test using OK:

python3 ok -q in -u
python3 ok -q in

Question 10

Define as-list, which takes a binary search tree and returns a sorted list containing the entries of the tree. For an optional challenge, define the procedure so that it runs in linear time in the length of the resulting list. Converting to and from the sorted list representation in linear time allows binary search trees to be intersected in linear time.

(define (as-list t)
    ; YOUR-CODE-HERE
    (else nil)
    )

You can unlock and test using OK:

python3 ok -q as-list -u
python3 ok -q as-list