Hannah and Je together

Wednesday, 28 July 2010

The Art of LISP Programming 02

Filed under: Lang:English,Subj:GNU/Computer — Jemyoung Leigh @ 3:29
Tags: , ,

Question 1-5 are about a simple database for names and addresses. This takes the form of a list which we shall call directory, obtained by repeatedly applying cons to lists of the form (name address). In turn, name and address are atoms.

For example, with entries:

Name Address
Eeyore Gloomy-place
Piglet beech-tree
Owl chestnut

dddNaddsdme Address
Eeyore Gloomy-place
Piglet beech-tree
Owl chestnut

directory becomes:

( ( ( Eeyore Gloomy-place)
    ( Piglet Beech-Tree)
    (Owl Chestnut)
  1. Represent the above as a diagram.
  2. How would you set up directory as shown above?
  3. What combination of head and tail functions will return Piglet’s address?
  4. How would you add the list d4: ( Pooh Mr-Saunders ) to the end of the list?
  5. Does ( setq directory1 ( cons d4 directory ) ) add Pooh’s entry to the list?
  6. What does ttail, defined as follows, do? ( defun ttail ( x ) ( tail ( tail x ) ) ) What do you get when it is applied to directory?
  7. Set up four to the list (1 2 3 4). What combination of head and tail return the atoms 1, 2, 3, 4?
  8. Initialise fred with ( setq fred ‘( (1 2) (3 4) ) ) What combinations of head and tail returns atoms 1, 2, 3, 4?
  9. Initialise emily with ( setq emily ‘( (1 2) (3 4) (5 6 7) ) ) Which of the following produce error messages? Otherwise, what is retured?
    1. (head(head(head emily)))
    2. (head(head emily))
    3. (head emily)
    4. (tail(head emily))
    5. (head(tail(head(tail emily))))
  10. If num is (1 2 3), how can you obtain ((1 2 3)) using cons?

This book defines the head and tail as:

(defun head (x) (car x))
(defun tail (x) (cdr x))

(1) I don’t want to do it.


(setq name1 'eeyore)
(setq name2 'piglet)
(setq name3 'owl)
(setq addr1 'gloomy-place)
(setq addr2 'beech-tree)
(setq addr3 'chestnut)
(setq d1 (cons name1 addr1))
(setq d2 (cons name2 addr2))
(setq d3 (cons name3 addr3))
(setq dir (cons d1 d2))
(setq directory (cons dir d3))


(tail (tail (head directory)))


(setq name4 'pooh)
(setq addr4 'mr-saunders)
(setq d4 (cons name4 addr4))
(setq directory (cons directory d4))

(5) I don’t think so. It’s different.

(6) Chestnut before the d4 is added to directory, Mr-Saunders after the d4 is added to directory


(setq four '(1 2 3 4))
(head four)
(head (tail four))
(head (tail (tail four)))
(head (tail (tail (tail four))))


(head (head fred))
(head (tail (head fred)))
(head (head (tail fred)))
(head (tail (head (tail fred))))


  1. Error
  2. 1
  3. (1 2)
  4. (2)
  5. 4


(setq num '(1 2 3))
(cons num NIL)

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

%d bloggers like this: