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 8: BASIC LIST PROCESSING
Notes
  1. Read Chapter 8.

  2. An easier way to build long lists is with the Common Lisp function list, which takes an arbitrary number of arguments, and returns a list of them. For example
    USER(1): (list 'a 1 'b)
    (A 1 B)
    
    USER(2): (list 'a 'b 'c '(d e) 'f 'g)
    (A B C (D E) F G)
    
    USER(3): (list 'a)
    (A)
    
    USER(4): (list)
    NIL
    
    Note carefully the difference between cons and list, illustrated by the following:
    USER(5): (cons 'a '(b c))
    (A B C)
    
    USER(6): (list 'a '(b c))
    (A (B C))
    
    USER(7): (cons 'a '())
    (A)
    
    USER(8): (list 'a '())
    (A NIL)
    
    cons adds its first argument as a new first element of the list which is its second argument. list makes a list whose elements are all its arguments.

  3. Create a file named ch8.cl into which you will put answers to just those selected exercises, I tell you to below.

  4. Do Exercises 8.1 and 8.2, and paste these interactions into ch8.cl Notice how easy it is to miss the dot in dotted pairs and dotted lists, but remember, if you ever generate a dotted pair or a dotted list as an answer for an exercise before we get to Chapter 18, it will be wrong!

  5. As Exercise 8.3, try for yourself those interactions of Chapter 8 (including those above) that you find particularly interesting or surprising. You needn't submit these.

  6. For Exercise 8.4, following the instructions, type forms to the Lisp listener that evaluate to the following lists:
    (1 2 "three")
    ((A B) C)
    (A (B C))
    (A (B) C)
    
    Make sure the values you get are exactly as shown. Paste these 4 interactions into ch8.cl As an example, here is an interaction that shows a form evaluating to ((1) "two" C)
    USER(9): (cons (cons 1 '()) (cons "two" (cons 'c '())))
    ((1) "two" C)
    

    Now do 8.4 again, but using list instead of cons. For example,

    USER(10): (list (list 1) "two" 'c)
    ((1) "two" C)
    

    Notice carefully that you are not to include quoted non-empty lists in any of the 8 forms you submit for this exercise. For example, the example immediately above is NOT

    USER(10): (list '(1) "two" 'c)
    ((1) "two" C)
    
    even though that gives the same result. If you have quoted non-empty lists in your answer, I will ask you to redo these exercises!

  7. Exercise 8.6: Don't do this exercise. It describes a technique you won't need as long as you are running acl within XEmacs. Instead, try out the following techniques:
    1. With your cursor after the Lisp prompt, type C-c C-p several times slowly. Each time, your previous input will be copied to the line where you are. You can then edit it or have Lisp evaluate it.

    2. With your cursor after the Lisp prompt, roll your mouse cursor over the text in the *acl* buffer slowly. If you copied my .emacs file, as I suggested, you should notice that Lisp objects are highlighted as you roll the mouse over them. Leaving the cursor where it is, and the mouse highlighting some Lisp list, type M-middle. (Hold the Meta key while clicking the middle mouse button.) The object your mouse is highlighting should be copied to the place your cursor is. This is a very fast way to copy text.

  8. Exercise 8.7: Using one of the techniques above (just so you don't have to retype that long list over and over), rather than the technique of the book's Exercise 8.6, enter forms that take the list given in the book, and evaluate to each of the following symbols: A, G, and I For example, here is such an interaction that evaluates to H:
    USER(6): (first (first (rest (rest '(((a b) (c d) e) (f g) (h) i)))))
    H
    
    Notice that each of your 3 answers will be a form that involves nested calls to first and rest, and has that long quoted list inside. Don't expect to come up with the three correct answers directly from your head---try something, then edit it, then edit it again, until it works correctly. Then paste the correct interaction into ch8.cl

    Make sure that your three forms evaluate to the symbols A, G, and I, rather than the lists (A), (G), and (I). I have gotten many such incorrect answers in the past.

  9. When you have all 14 interactions (1 for 8.1, 2 for 8.2, 8 for 8.4, and 3 for 8.7) pasted into ch8.cl, submit that file, and go on to Chapter 9.

Next

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

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