Next: Truth, Previous: Data, Up: Welcome to Lisp [Contents][Index]
The function cons
builds lists. If its second argument is a list, it returns
a new list with the first argument added to the front.
> (cons ’a ’(b c d)) (A B C D)
We can build up lists by consing new elements onto an empty list. The list
function is just a more convenient way of consing several things onto nil
.
> (cons ’a (cons ’b nil)) (A B) > (list ’a ’b) (A B)
The primitive functions for extracting the elemnets of lists are car
and
cdr
. The car
of a list is the first element, and the cdr
is everything
after the first element.
> (car ’(a b c)) A > (cdr ’(a b c)) (B C)
You can use combinations of car
and cdr
to reach any element of a list. If
you want to get the third element, you could say:
> (car (cdr (cdr ’(a b c d)))) C
You can do the same thing by calling third
:
> (third ’(a b c d)) C