Saturday, June 25, 2005

Day 2: cons, car and cdr and more on lists

The following is a list in Lisp:

("foo" "bar" "baz")

You can construct this list in several ways. One way is to type in:

(list "foo" "bar" "baz")

The other way to get the same list is supposed to be with cons like:

(cons "foo" "bar")

The book seems to indicate that it should evaluate to:

("foo" "bar")

But I get

("foo" . "bar")


There seems to be a difference between the output expected from the book and that which I get from LispWorks. The implementation of Lisp that I am using when on a Windows machine is LispWorks Personal Edition. Maybe this explains the difference.

Anyway, cons is symetrical with a combination of car and cdr functions:

(cons (car (list 1 2 3)) (cdr (list 1 2 3)))

evaluates to

(1 2 3)

because

(car (list 1 2 3)) => 1
(cdr (list 1 2 3)) => (2 3)

and cons stitches them back into a list.

first and rest seem to be synonyms for car and cdr.

No comments: