Go to the previous, next section.

 setcar

As you might guess from their names, the  setcar and  setcdr functions set the  car or the  cdr of a list to a new value. They actually change the original list, unlike  car and  cdr which leave the original list as it was. One way to find out how this works is to experiment. We will start with the  setcar function.

First, we can make a list and then set the value of a variable to the list, using the  setq function. Here is a list of animals:

(setq animals '(giraffe antelope tiger lion))

If you are reading this in Info inside of GNU Emacs, you can evaluate this expression in the usual fashion, by positioning the cursor after the expression and typing C-x C-e. (I'm doing this right here as I write this. This is one of the advantages of having the interpreter built into the computing environment.)

When we evaluate the variable  animals , we see that it is bound to the list  (giraffe antelope tiger lion) :

animals
     @result{} (giraffe antelope tiger lion)

Put another way, the variable  animals points to the list  (giraffe antelope tiger lion) .

Next, evaluate the function  setcar while passing it two arguments, the variable  animals and the quoted symbol  hippopotamus ; this is done by writing the three element list  (setcar animals 'hippopotamus) and then evaluating it in the usual fashion:

(setcar animals 'hippopotamus)

After evaluating this expression, evaluate the variable  animals again. You will see that the list of animals has changed:

animals
     @result{} (hippopotamus antelope tiger lion)

The first element on the list,  giraffe is changed to  hippopotamus .

So we can see that  setcar did not add a new element to the list as  cons would have; it replaced  giraffe with  hippopotamus ; it changed the first element of the list.

Go to the previous, next section.