- Access to the head (
car) element of the list, or- To the tail (
cdr) of the list, which is a list itself.

Consequently most LISP functions have the form:

- What to do with the head (
car) element;- What to do with the tail (
cdr) of the list.

For example, let us develop the function to find the last element of a list.

The pseudocode could be:

To find the last item in a list,

- if the list is only one element long, then the last element is the head element;
- else find the last element of the tail.

How do we test to see if a list is of length 1? If the list is not an empty list, then the list is of length 1 when **(cdr list)** is null. Note that we need to eliminate the empty list case first. So our definition of the **last** function is:

( define last (list) (cond ((null list) nil) % eliminate the empty list case ((null (cdr list)) (car list)) % when the list is length 1 take first element (t (last (cdr list))) % otherwise get the last of the tail ) )

Last updated 2001/04/06

© J.A.N. Lee, 2001.