Lists
Lists can be built using pairs. A list is denoted by a collection of items enclosed in parentheses, e.g.
(2 4 6 8 10)
In other words, a list is a chain of pairs ending in the empty list. If the chain of pairs does not end with the empty list, the list is said to be improper. An improper list is not a list!
The empty list is denoted (). The list (2 4 6 8 10) is the same as (2 . (4 . (6 . (8 . (10 . ()))))). An improper list can be represented using dotted notations in the following way: (2 4 6 8 . 10), which is equivalent to (2 . (4 . (6 . (8 . 10)))).
Note that lists should be quoted when fed to the interpreter, otherwise the interpreter will try to apply the first item in the list to the other items in the list, e.g.
> (2 4 6 8 ) procedure application: expected procedure, given: 2; arguments were: 4 6 8 > '(2 4 6 8 ) (2 4 6 8 )
Lists can be constructed using cons in the following way:
(cons 2 (cons 4 (cons 6 (cons 8 (cons 10 '())))))
Note that
(cons 1 (cons 2 '()))
is a list, whereas
(cons 1 2)
is not. The latter is a pair. A list is constructed from pairs and is consiedered a pair, but a pair is not in itself a list! The empty list is, however, not a pair.
Lists can also be constructed using the procedure list as follows:
(list 2 4 6 8 10)
If we wish to take lists apart, we can use car and cdr and combinations thereof. For example:
>(car (list 2 4 6 8 10)) 2
whereas
>(cdr (list 2 4 6 8 10)) (4 6 8 10)
The result of applying cdr to a list is always a list.
Let’s say that we want to extract the second element in the list. In order to accomplish this we can combine car and cdr in the following way:
>(car (cdr (list 2 4 6 8 10))) 4
There is a shortening for combined car:s and cdr:s. The same could be accomplished as follows:
>(cadr (list 2 4 6 8 10)) 4
car:s and cdr:s can be combined four times. Hence, the following combinations exist:
(caar ls) ; is the same as (car (car ls)) (cadr ls) ; is the same as (car (cdr ls)) (cdar ls) ; is the same as (cdr (car ls)) (cddr ls) ; is the same as (cdr (cdr ls)) (caaar ls) ; is the same as (car (car (car ls))) (caadr ls) ; is the same as (car (car (cdr ls))) (cadar ls) ; is the same as (car (cdr (car ls))) (caddr ls) ; is the same as (car (cdr (cdr ls))) (cdaar ls) ; is the same as (cdr (car (car ls))) (cdadr ls) ; is the same as (cdr (car (cdr ls))) (cddar ls) ; is the same as (cdr (cdr (car ls))) (cdddr ls) ; is the same as (cdr (cdr (cdr ls))) (caaaar ls); is the same as (car (car (car (car ls)))) (caaadr ls); is the same as (car (car (car (cdr ls)))) (caadar ls); is the same as (car (car (cdr (car ls)))) (caaddr ls); is the same as (car (car (cdr (cdr ls)))) (cadaar ls); is the same as (car (cdr (car (car ls)))) (cadadr ls); is the same as (car (cdr (car (cdr ls)))) (caddar ls); is the same as (car (cdr (cdr (car ls)))) (cadddr ls); is the same as (car (cdr (cdr (cdr ls)))) (cdaaar ls); is the same as (cdr (car (car (car ls)))) (cdaadr ls); is the same as (cdr (car (car (cdr ls)))) (cdadar ls); is the same as (cdr (car (cdr (car ls)))) (cdaddr ls); is the same as (cdr (car (cdr (cdr ls)))) (cddaar ls); is the same as (cdr (cdr (car (car ls)))) (cddadr ls); is the same as (cdr (cdr (car (cdr ls)))) (cdddar ls); is the same as (cdr (cdr (cdr (car ls)))) (cddddr ls); is the same as (cdr (cdr (cdr (cdr ls))))
For example:
>(caddr (list 2 4 6 8 10)) 6 >(car (cdr (cdr (list 2 4 6 8 10)))) 6
These procedures can also be combined if the standards ones are insufficient. For example:
> (define ls '(((((((1 2) 3) 4) 5) 6) 7) 8)) > (caaaar ls) (((1 2) 3) 4) > (car (caaaar ls)) ((1 2) 3) > (cdaar (caaaar ls)) (2)
Scheme also provides the procedures set-car! and set-cdr! for explicitly changing the value of the car or cdr of a list. This is done as a side effect and the return value is unspecified.
> (define ls (list 1 2 3 4 5)) > ls (1 2 3 4 5) > (set-car! ls 10) > ls (10 2 3 4 5) > (set-cdr! ls '()) > ls (10)
Note that lists created in the following way cannot be changed using set-car! or set-cdr!:
> (define ls '(1 2 3 4 5))
This is because ls is now a list constant that cannot be changed. Don’t be tempted to do it incorrectly even if the implementation allows it.
Care should be taken when using set-car! and set-cdr!. For example: when using set-cdr!, we may run into situations in which the object is a list at first, but after application of set-cdr! it is not.
Posted by munchy
Posted by munchy
Posted by munchy