Next: , Previous: , Up: Conses Dictionary  


14.2.9 car, cdr,

caar, cadr, cdar, cddr,

caaar, caadr, cadar, caddr, cdaar, cdadr, cddar, cdddr,

caaaar, caaadr, caadar, caaddr, cadaar, cadadr, caddar, cadddr,

cdaaar, cdaadr, cdadar, cdaddr, cddaar, cddadr, cdddar, cddddr

[Accessor]

car xobject (setf (car x) new-object)

cdr xobject (setf (cdr x) new-object)

\vksip 5pt xobject (setf (\vksip 5pt x) new-object)

caar xobject (setf (caar x) new-object)

cadr xobject (setf (cadr x) new-object)

cdar xobject (setf (cdar x) new-object)

cddr xobject (setf (cddr x) new-object)

\vksip 5pt xobject (setf (\vksip 5pt x) new-object)

caaar xobject (setf (caaar x) new-object)

caadr xobject (setf (caadr x) new-object)

cadar xobject (setf (cadar x) new-object)

caddr xobject (setf (caddr x) new-object)

cdaar xobject (setf (cdaar x) new-object)

cdadr xobject (setf (cdadr x) new-object)

cddar xobject (setf (cddar x) new-object)

cdddr xobject (setf (cdddr x) new-object)

\vksip 5pt xobject (setf (\vksip 5pt x) new-object)

caaaar xobject (setf (caaaar x) new-object)

caaadr xobject (setf (caaadr x) new-object)

caadar xobject (setf (caadar x) new-object)

caaddr xobject (setf (caaddr x) new-object)

cadaar xobject (setf (cadaar x) new-object)

cadadr xobject (setf (cadadr x) new-object)

caddar xobject (setf (caddar x) new-object)

cadddr xobject (setf (cadddr x) new-object)

cdaaar xobject (setf (cdaaar x) new-object)

cdaadr xobject (setf (cdaadr x) new-object)

cdadar xobject (setf (cdadar x) new-object)

cdaddr xobject (setf (cdaddr x) new-object)

cddaar xobject (setf (cddaar x) new-object)

cddadr xobject (setf (cddadr x) new-object)

cdddar xobject (setf (cdddar x) new-object)

cddddr xobject (setf (cddddr x) new-object)

Pronunciation::

cadr: pronounced ’ka ,de r

caddr: pronounced ’kad e ,de r or pronounced ’ka ,dude r

cdr: pronounced ’ku ,de r

cddr: pronounced ’kud e ,de r or pronounced ’ke ,dude r

Arguments and Values::

x—a list.

object—an object.

new-object—an object.

Description::

If x is a cons, car returns the car of that cons. If x is nil, car returns nil.

If x is a cons, cdr returns the cdr of that cons. If x is nil, cdr returns nil.

Functions are provided which perform compositions of up to four car and cdr operations. Their names consist of a C, followed by two, three, or four occurrences of A or D, and finally an R. The series of A’s and D’s in each function’s name is chosen to identify the series of car and cdr operations that is performed by the function. The order in which the A’s and D’s appear is the inverse of the order in which the corresponding operations are performed. Figure 14–6 defines the relationships precisely.

  This place ...  Is equivalent to this place ...  
  (caar x)        (car (car x))                    
  (cadr x)        (car (cdr x))                    
  (cdar x)        (cdr (car x))                    
  (cddr x)        (cdr (cdr x))                    
  (caaar x)       (car (car (car x)))              
  (caadr x)       (car (car (cdr x)))              
  (cadar x)       (car (cdr (car x)))              
  (caddr x)       (car (cdr (cdr x)))              
  (cdaar x)       (cdr (car (car x)))              
  (cdadr x)       (cdr (car (cdr x)))              
  (cddar x)       (cdr (cdr (car x)))              
  (cdddr x)       (cdr (cdr (cdr x)))              
  (caaaar x)      (car (car (car (car x))))        
  (caaadr x)      (car (car (car (cdr x))))        
  (caadar x)      (car (car (cdr (car x))))        
  (caaddr x)      (car (car (cdr (cdr x))))        
  (cadaar x)      (car (cdr (car (car x))))        
  (cadadr x)      (car (cdr (car (cdr x))))        
  (caddar x)      (car (cdr (cdr (car x))))        
  (cadddr x)      (car (cdr (cdr (cdr x))))        
  (cdaaar x)      (cdr (car (car (car x))))        
  (cdaadr x)      (cdr (car (car (cdr x))))        
  (cdadar x)      (cdr (car (cdr (car x))))        
  (cdaddr x)      (cdr (car (cdr (cdr x))))        
  (cddaar x)      (cdr (cdr (car (car x))))        
  (cddadr x)      (cdr (cdr (car (cdr x))))        
  (cdddar x)      (cdr (cdr (cdr (car x))))        
  (cddddr x)      (cdr (cdr (cdr (cdr x))))        

         Figure 14–6: CAR and CDR variants        

setf can also be used with any of these functions to change an existing component of x, but setf will not make new components. So, for example, the car of a cons can be assigned with setf of car, but the car of nil cannot be assigned with setf of car. Similarly, the car of the car of a cons whose car is a cons can be assigned with setf of caar, but neither nil nor a cons whose car is nil can be assigned with setf of caar.

The argument x is permitted to be a dotted list or a circular list.

Examples::

 (car nil) ⇒  NIL  
 (cdr '(1 . 2)) ⇒  2
 (cdr '(1 2)) ⇒  (2)
 (cadr '(1 2)) ⇒  2 
 (car '(a b c)) ⇒  A
 (cdr '(a b c)) ⇒  (B C)

Exceptional Situations::

The functions car and cdr should signal type-error if they receive an argument which is not a list. The other functions (caar, cadr, ... cddddr) should behave for the purpose of error checking as if defined by appropriate calls to car and cdr.

See Also::

rplaca , first , rest

Notes::

The car of a cons can also be altered by using rplaca, and the cdr of a cons can be altered by using rplacd.

(car x)    ≡ (first x)
(cadr x)   ≡ (second x) ≡ (car (cdr x))
(caddr x)  ≡ (third x)  ≡ (car (cdr (cdr x)))
(cadddr x) ≡ (fourth x) ≡ (car (cdr (cdr (cdr x))))

Next: , Previous: , Up: Conses Dictionary