The Department of Computer Science & Engineering
cse@buffalo

CSE202: Programming in Lisp

Course
Grades
Email

Welcome

Policies
    Grades
    Inc
    Intgrty

Preface
Part I
  Chap 1
  Chap 2
  Chap 3
  XEmacs
  Chap 4
  Chap 5
  Chap 6
  Chap 7
  Chap 8
  Chap 9
Part II
  Chap 10
  Chap 11
  Chap 12
  Chap 13
  Chap 14
  Chap 15
  Chap 16
  Chap 17
  Chap 18
  Chap 19
  Chap 20
  Chap 21
  Chap 22
  Chap 23
Part III
  Chap 24
  Chap 25
  Chap 26
  Chap 27
  Chap 28
  Chap 29
  Chap 30
  Chap 31
  Chap 32
CHAPTER 28: ITERATION
Corrections
  1. Page 207, line 10: Change 12))) to 12)).

  2. Page 207, line 17: Change forms to statements.

  3. Page 207, line 18: Change form to statement.

  4. Page 210, line -1: Change list:dolist to lisp:dolist.

Notes
  1. Read Chapter 28

  2. As I say in the footnote on page 204, ANSI Common Lisp has a new, very involved loop, but the simple loop discussed in this chapter still works as I describe it.

  3. The one branch conditionals when and unless are discussed on page 205. You should immediately start using them whenever they are appropriate.

  4. In Exercise 28.1, you are asked to define a function that returns a list of 100 elements. If you just do this and test it, Lisp will not print the entire list for you. There are several global variables that control various aspects of printing, two of which are:
    • If the value of top-level:*print-length* is an integer n, only n elements of a list will be printed, followed by "..."; if its value is NIL, all elements of lists will be printed. The default value is 10.

    • If the value of top-level:*print-level* is an integer n, trees will be printed only to a depth of n followed by "#"; if its value is NIL, trees will be printed to their full depth. The default value is 5.
    You may change these variables with setf. The reason not to make them both NIL is that, through various errors, it is possible to construct infinite long lists and infinitely deep trees, and you don't want to make Lisp try to print them fully.

  5. Make a file named ch28.cl. Paste the following into it. Replace the question marks with your answers, and submit the file.
    ----------------------- cut here -----------------------
    ;;; Answers To Chapter 28 Exercises
    ;;; by ???
    ;;; Username: ???
    
    (defpackage ch28)
    
    (in-package ch28)
    
    (setf top-level:*print-length* 125)
    
    ;;; 28.1 The definition of build-list is
    ???
    
    ;;; 28.2 (supplied)
    (defun reverse1 (l)
      "Returns a copy of the list L
       with the order of members reversed."
      (check-type l list)
      (reverse2 l '())) 
    
    (defun reverse2 (l1 l2)
      "Returns a list consisting of
       the members of L1 in reverse order
       followed by the members of L2 in original order."
      (check-type l1 list)
      (check-type l2 list)
      (if (null l1) l2
          (reverse2 (rest l1)
                    (cons (first l1) l2))))
    
    ;;; 28.3 (supplied)
    (defun reverse3 (l)
      "Returns a copy of the list L
       with the order of members reversed."
      (check-type l list)
      (let (l2)
        (loop
          (when (null l) (return l2))
          (push (pop l) l2))))
    
    (defun reverse4 (l)
      "Returns a copy of the list L
       with the order of members reversed."
      (check-type l list)
      (let (l2)
        (mapc #'(lambda (e) (push e l2))
              l)
        l2))
    
    (defun reverse5 (l)
      "Returns a copy of the list L
       with the order of members reversed."
      (check-type l list)
      (let (l2)
        (dolist (e l l2)
          (push e l2))))
    
    ;;; 28.4 Note that you should not quote the argument of time.  That
    ;;;      is, to time reverse1, you should evaluate
    ;;;              (time (reverse1 longlist))
    ;;;      You should also make longlist long enough to see interesting
    ;;;      differences in the running times of the 5 versions of reverse.
    ;;;      100 might not be long enough.
    ;;;      What length did you use?
    ???
    ;;;      The 5 timing tests are shown here:
    ???
    
    ;;; 28.5 The form assigning the variable reverses the value requested
    ;;;      in the text, the dolist form, and its results are shown here:
    ???
    ???
    ???
    
    ;;; 28.6 The form mapping compile over the five functions (reverse1,
    ;;;      reverse2, reverse3, reverse4, and reverse5) and the value of
    ;;;      that mapping is shown here:
    ???
    ???
    
    ;;; 28.7 The timing test of the compiled functions is shown here:
    ???
    
    ;;; The following table shows the results of the two timing tests.
    ;;; (Fill in each "time" column with the appropriate number of "cpu
    ;;;  time (non-gc) msecs", and fill in each "order" column with
    ;;;  integers 1 - 5, 1 for the fastest function,
    ;;;  5 for the slowest function.  In case of ties assign the same integer
    ;;;  to all tied functions.)
    ;;; 
    ;;;			before compile		after compile
    ;;;			time	order		time	order
    ;;; reverse1		???	 ???		???	 ???
    ;;; reverse3		???	 ???		???	 ???
    ;;; reverse4		???	 ???		???	 ???
    ;;; reverse5		???	 ???		???	 ???
    ;;; lisp:reverse	???	 ???		???	 ???
    
    ;;; 28.17  Could you find a way to make the form True other than by
    putting a form that contains s itself in the blank?  If so, how?
    ???
    
    ----------------------- cut here -----------------------
    

  6. When you have submitted correct exercises through this chapter, you will have earned a grade of A-.

Next

Copyright © 1999, 2000 by Stuart C. Shapiro. All rights reserved.

Stuart C. Shapiro <shapiro@cse.buffalo.edu>