John Perks <jjap2@cam.ac.uk> wrote:
+---------------
| Is it true that Lisp's empty list, unlike Scheme's, has car and cdr equal
| to itself? This seems to be true of the Lisps I've tried it on, but what
| the Standard say?
+---------------
As Barry said, for CL (c{a,d}r nil) ==> nil, but note that this does *not*
imply that the empty list is necessarily implemented as some distinguished
cons cell which has both its car & cdr pointers pointing to itself [despite
some early implementations which did exactly that]. In fact, in neither
Scheme nor CL is the empty list *permitted* to be a [user-level] cons cell:
% clisp
> (consp '())
NIL
> ^D
% mzscheme
> (pair? '())
#f
> ^D
%
The difference is that Scheme requires the argument of "car" or "cdr"
to be a pair (cons cell), while CL requires the argument of "car" or "cdr"
to be *either* a cons cell or the empty list -- and in the latter case the
standard happens to *define* the value of the operation to be the empty list.
One could get the same behavior in Scheme, if you like, by redefining the
built-in "car" & "cdr" [purists, please, no flames!] as follows:
> (car '())
car: expects argument of type <pair>; given ()
> (let ((original-car car))
(set! car
(lambda (x)
(cond
((pair? x) (original-car x))
((null? x) x)
(else (error "car: not a list:" x))))))
> (car '())
()
>
-Rob
-----
Rob Warnock, 41L-955 rpw3@sgi.com
Applied Networking http://reality.sgi.com/rpw3/
Silicon Graphics, Inc. Phone: 650-933-1673
1600 Amphitheatre Pkwy. PP-ASEL-IA
Mountain View, CA 94043