car, cdr, and others

car, cdr, and others — return specific elements of a list.

Syntax

car (list)
cdr (list)
caar (list)
cadr (list)
cdar (list)
cddr (list)
caaar (list)
caadr (list)
cadar (list)
caddr (list)
cdaar (list)
cdadr (list)
cddar (list)
cdddr (list)

		

Arguments

list

Any list.

Returns

An element of the list or nil.

Description

The car function returns the first element of a list. The cdr function returns all of the list except for the first element. The remaining functions in this group are simply shortcuts for the common combinations of car and cdr. The shortcut functions are read from left to right as nested car and cdr calls. Thus, a call to caddr (mylist) would be equivalent to car (cdr (cdr (mylist))). If the argument is not a list, the result is nil. The cdr function will only return a non-list result in the case of a dotted pair.

Example

Gamma> car(list(1,2));
1
Gamma> cdr(list(1,2));
(2)
Gamma> cdr(cons(1,2));
2
Gamma> caadr (list (1, list(2, 3, list(4, 5), 6)));
2
Gamma> cdadr (list (1, list(2, 3, list(4, 5), 6)));
(3 (4 5) 6)
Gamma> 
		

See Also

cons, list, nth_car, nth_cdr