(define atom? (lambda (x) (and (not (pair? x)) (not (null? x))))) ; lat? ; for "is list of atoms?" ; from *The Little Schemer*, page 19. (define lat? (lambda (l) (cond ((null? l) true) (else (and (atom? (car l)) (lat? (cdr l))))))) ; member? ; from *The Little Schemer*, page 22. (define member? (lambda (a lat) (cond ((null? lat) false) (else (or (eq? (car lat) a) (member? a (cdr lat))))))) ; length1 ; this function is from *The Little Schemer*, page 76. ; don't worry that it occurs so late there. ; it counts how many elements there are in a list. (define length1 (lambda (lat) (cond ((null? lat) 0) (else (+ 1 (length1 (cdr lat))))))) ; length2 ; this is a different way to write length. ; now we use a helper function to count how many ; elements there are in a list. ; the helper function counts how many elements ; we've see so far. (define help-length2 (lambda (lat ct-so-far) (cond ((null? lat) ct-so-far) (else (help-length2 (cdr lat) (+ 1 ct-so-far)))))) (define length2 (lambda (lat) (help-length2 lat 0))) ; you can step through these functions to see the difference. ; which program is easier to understand when you read it? ; which program is easier to understand when you run it? (length1 '(chris kim sandy)) (length2 '(chris kim sandy)) ; here is the function that we wrote in class ; to compute the largest element in a list. ; the helper function takes the largest ; element we've seen so far, and the rest ; of the list, and returns the largest element ; in the whole list. (define maximum-recursive-part (lambda (max-so-far rest) (cond ((null? rest) max-so-far) (else (maximum-recursive-part (if (> (car rest) max-so-far) (car rest) max-so-far) (cdr rest)) )))) ; the overall function initializes it. ; the thing you have to decide here is ; the answer to (maximum '()) ; here we set the answer to be negative ; infinity (define maximum (lambda (l) (maximum-recursive-part -inf.0 l))) ; here, (maximum2 '()) will return an error (define maximum2 (lambda (l) (maximum-recursive-part (car l) (cdr l)))) (maximum '(1 8 2)) (maximum2 '(1 8 2))