> (define lista '(1 2 5 67 3 2 5 88)) > (define fruits '(apple pear orange banana)) > (define colors '(red blue green yellow orange)) > (car lista) 1 > (car colors) red(cdr list) returns the list consisting of all but the first element.
> (cdr lista) (2 5 67 3 2 5 88) > (cdr colors) (blue green yellow orange)You can't take the car or cdr of the empty list.
> (car '()) car: expects argument of type <pair>; given () > (cdr '()) cdr: expects argument of type <pair>; given ()There are convenient variants that let you take lists apart. For example, (cadar list) is the car of the cdr of the car of list, so if you need it, you don't have to write (car (cdr (car list))).
> (cadr colors) blue > (cadr fruits) pear > (caddr fruits) orange > (define prices '((banana 0.98) (orange 0.33) (lemon 0.20))) prices > (car prices) (banana 0.98) > (caar prices) banana > (cadar prices) 0.98 > (cdar prices) (0.98) > (cdddr fruits) (banana)(cons element list) returns what you get when you stick the element on to the beginning of the list. It is important to note that we don't change any values unless we do a define.
> (cons 'lemon fruits) (lemon apple pear orange banana) > fruits (apple pear orange banana) > (define fruits (cons 'lemon fruits)) > fruits (lemon apple pear orange banana) > (cons 'black (cdr colors)) (black blue green yellow orange) > colors (red blue green yellow orange)We can use these to build functions that manipulate lists. remove is a typical tail-recursive function on lists. The recursion terminates either when we reach the end of the list, or when we find a match for the element we want to remove. If the first element of the list is the one we want to throw away, we just return the rest of the list. Otherwise, we keep the first element and cons it to what we get from recursively looking at the rest of the list.
> (define (remove element list) ; remove the first occurrence, if any, of the element from the list (cond ((null? list) list) ((equal? element (car list)) (cdr list)) (else (cons (car list) (remove element (cdr list)))) ) ) > (remove 'orange fruits) (lemon apple pear banana) > (remove 'tomato fruits) (lemon apple pear orange banana)Our next example function removes all occurrences of the element, not just the first one.
> (define (removeall element list) ; remove all occurrences of the element from the list (cond ((null? list) list) ((equal? element (car list)) (removeall element (cdr list))) (else (cons (car list) (removeall element (cdr list)))) ) ) > lista (1 2 5 67 3 2 5 88) > (removeall 5 lista) (1 2 67 3 2 88)Now we show how to build the list of factors of an integer > 1. First we have a simple function that decides when a is divisible by b.
> (define (divisible? a b) (zero? (remainder a b)))Now how do we find all the factors of n? We start with 2; if n is divisible by 2, we add 2 to the list of factors, divide n by 2, and try 2 again. Otherwise we try 3. If 3 is a factor, we add it to the list, divide n by 3, and try 3 again. Otherwise we try 5. We go on, trying 5, 7, 9,... until the number we are trying has a square greater than whatever is left of n. When that happens, we return the remaining factor - in a list.
(define nil '())
(define (factors n) ; return the list of factors of n (cond ((< n 2) nil) (else (factor-aux n 2)) ) ) (define (factor-aux n try) (cond ? ((> (square try) n) (list n)) ; quit when try > sqrt(n) ((divisible? n try) ; add a factor, get the rest (cons try (factor-aux (quotient n try) try))) (else (factor-aux ? n ; try another factor (cond ((= try 2) 3)(else (+ try 2))))) ) ) factor-aux > (factors 8) (2 2 2) > (factors 143) (11 13) > (factors 3333) (3 11 101) > (factors 1298664) (2 2 2 3 3 17 1061) > (factors 8388607) (47 178481)There is a new function in the above; (list a b c ...) returns the list of its arguments.
> (list 2) (2) > (list 4 5 6 7) (4 5 6 7) > (define colors '(red yellow blue green purple)) colors > (list colors colors) ((red yellow blue green purple) (red yellow blue green purple)) > (cons colors colors) ((red yellow blue green purple) red yellow blue green purple)Notice that last line - cons stuck one thing on as an element.
Here are some more examples to illustrate recursion:
(define (firstn list n) ; first n elements of the list (cond ((= n 0) nil) (else (cons (car list) (firstn (cdr list) (- n 1)))) ) ) (define (delistify list) ; remove all parentheses from inside the list (cond ((null? list) nil) ((list? (car list)) (append (delistify (car list)) (delistify (cdr list)))) (else (cons (car list) (delistify (cdr list)))) ) )
> (firstn a 5) (1 2 3 2 3) > (define b '(1 (2 3 (4)) 5 ((6 7 (8 9) 10)))) > b (1 (2 3 (4)) 5 ((6 7 (8 9) 10)))
> (delistify b)
(1 2 3 4 5 6 7 8 9 10)