CS 3723/3721
Programming Languages
Lisp Internal Representation
and Dotted Pairs
|
Lisp S-expressions:
- A lisp S-expression S is defined recursively:
An S-expression S is either
- an atom, or
- a parenthesized list of
zero of more S-expressions.
Internal Representation of S-expressions:
Now look at the representation for (a b c)
above.
The main pointer points to the first (leftmost) lisp cell.
The car pointer of this lisp cell points to
a,
while the cdr pointer points to
(b c).
Thus (car '(a b c)) is
a, and
(cdr '(a b c)) is
(b c).
Now suppose we want to create the result of the expression:
(cons 'a '(b c)).
To do this, take the representations for a
and for (b c).
Create a new lisp cell, stick a pointer to the first representation above
in as the car pointer,
and stick a pointer to the second representation above
in as the cdr pointer.
The result is the representation for
(a b c).
Above, where ---> nil
is written, this really means that the cdr pointer
is just an actual null pointer.
Dotted Pair Notation in Lisp:
This is the dotted pair notation in Lisp, something
you haven't seen before.
Internally, this is represented as follows:
+---+---+
(a . b): --->| o | o-|---> b
+---+---+
|
v
a
The dotted pair notation shows the lisp cells more explicitly.
Consider the following interactive session in Lisp:
> '(a . b)
(A . B)
> '(a . nil)
(A)
> '(a . (b . nil))
(A B)
> '(a . (b . (c . nil)))
(A B C)
> '(a . ((b . (c . nil)) . (d . nil)))
(A (B C) D)
> (cons 'a 'nil)
(A)
> (cons 'a (cons 'b 'nil))
(A B)
> '(a . ((b . nil) . c))
(A (B) . C)
Two points about the dotted notation:
- There must always be a space between a dot and an atom.
(Just as there must be a space between two atoms.)
- In displaying S-expressions, Lisp uses as few dots as possible.
(Illustrated especially with the last item above.)
Revision date: 2004-03-19.
(Please use ISO 8601,
the International Standard Date and Time Notation.)