tags:

views:

348

answers:

3

When I make a list in Scheme, what does the cdr of the last pair point to? Is it an 'empty word' or an 'empty list'? DrScheme doesnt mind (cons 'a empty) nor (cons 'a '()). Finally what is the difference between the empty word and the empty list?

+1  A: 

The cdr of the last pair points to '(), the empty list.

Nathaniel Flath
why does (cons 'a empty) work?
kunjaan
All lists are made up of a series of lists, each of length 2. For example, (1 2 3) is actually a list of length 2: the first item is 1, and the second item is (2 3). That inner list is also a list of length 2: the first item is 2, and the second item is (3). That second item is actually a two-item list as well: the first item is 3, and the second item is ().So, cons('a '()) returns ('a), because that is actually ('a . '()).
Matt Poush
+1  A: 

The answer to all of your questions can be found if you evaluate the symbol empty. It's defined to be equal to '(), the empty list, and this is what the last cdr points to. To convince yourself, try this:

(cdr (list 1 2 3))
(cdr (cdr (list 1 2 3)))
(cdr (cdr (cdr (list 1 2 3))))
Iceman
Although it answers a part of my question, I just realized that the cdr need not point to empty. I could have an improper list like (a b .d). Thanks anyway.
kunjaan
+4  A: 

DrScheme doesn't mind your cons-ing, because cons can do more than just build lists. From the reference,

(cons a d) → pair?
  a : any/c
  d : any/c

which means that cons builds pairs. It's also perfectly legal to do

> (cons 1 2)
(1 . 2)

which is not a list, but which still lets you do

> (car (cons 1 2))
1
> (cdr (cons 1 2))
2

But anyways, the "empty word" and the "empty list " are the same object, as can be checked by

> (eq? empty '())
#t
othercriteria