ICS H21 • UC IRVINE • DAVID G. KAY • FALL 2008

Fifth Homework


This assignment is due at the start of lab on Monday, October 27.

(1) A fresh copy of the midterm is available here. Answer all the questions correctly, including the extra credit, and turn your answers in. For any problem that you got full credit for on the actual midterm, you can simply write "I got full credit for this the first time around."

We're not trying to make anyone relive unpleasant memories; the midterm scores were generally quite good. But everyone needs to know how to do all the problems on the midterm, and you need to test yourself to make sure that's true for you. It's fine if you've discussed the midterm in section, but you should try to answer each question without consulting your notes or anyone else. You're not forbidden from asking your classmates for help, but this isn't a pair-programming activity; you need to be confident that you can do each of these problems yourself.

Turn in your original graded midterm to the TA in section along with the correctly answered copy; try to do this by Monday.

(2) Chapter 15 of the HtDP text, which was assigned but not included in the lab assignment last week, covers data structures that are "mutually recursive"—an X might contain some Ys, and Ys in turn could contain Xs. A related example appears below.

;; Example of nested lists (lists containing other lists)
;; Data definition:  A book is either
;; -- empty, or
;; -- (cons symbol book), where a symbol represents a word, or
;; -- (cons book book)
;; So this is a book:
(define TTC '(A Tale of Two Cities
                 (It was the best of times)
                 (It was the Dover road)))
;; This gives us a hierarchical (tree-shaped) organization;
;; each nested list could be a chapter, which itself could
;; contain lists for each section or paragraph.
;; But suppose we want to count the words in this book.
;; word-count:  book -> number
;; Return number of words in book
#|
(check-expect (word-count TTC) 16)
(check-expect (word-count empty) 0)
|#
;; We can't just say (length TTC); that gives us the number
;; of elements at the top level (7 in this case), counting
;; each nested list as a single item.  Instead, we need to
;; look into each nested list.  We can write the code for 
;; this by following the data definition, which has three
;; parts:  The list is empty, the first item is a symbol,
;; or the first item is a list:
#|
(define word-count
  (lambda (B)
    (cond
      ((empty? B) ...)
      ((symbol? (first B)) ...)
      (else ...))))
|#
;; If the book is empty, it has zero words.
;; If the first item is a word (a symbol), add 1 to the
;; number of words in the rest of the book.
;; If the first item is a book (a list), add the number
;; of words in that book to the number of words in the 
;; rest of the book
(define word-count
  (lambda (B)
    (cond
      ((empty? B) 0)
      ((symbol? (first B)) (add1 (word-count (rest B))))
      (else (+ (word-count (first B)) (word-count (rest B)))))))
;; Notice that there's a recursive call in this code
;; at exactly the same places as "book" appears in the
;; data definition above.
(check-expect (word-count TTC) 16)
(check-expect (word-count empty) 0)

(3) Chapter 16 (also assigned but not included in the lab assignment) goes through a longer example of the development process (using file management). It's also another illustration of defining and processing tree-shaped structures.

(4) Chapter 17 talks about handling functions that process two different kinds of complex data. Do exercises 17.1.1, 17.1.2, 17.2.1, 17.2.2, and 17.3.1.

(5) Intermezzo 3 (Chapter 18) covers local definitions, which we saw in the restaurants program. Do exercises 18.1.1, 18.1.2, 18.1.3, 18.1.5, and 18.1.15.

Based in part on ICS H21assignments and exams by David G. Kay from Fall 2001; modified by David G. Kay, Fall 2004, Fall 2005, Fall 2008.


David G. Kay, kay@uci.edu
Friday, October 24, 2008 7:03 AM