cdr’ing down a list
examples of procedures with the type (list of int) –> int
- procedure that sums the elements of a list
(define (sum p) (if (null? p) 0 (+ (car p) (sum (cdr p))))
- procedure that multiplies the elements of alist
(define (product p) (if (null? p) 1 (* (car p) (product (cdr p))))
- procedure that counts the number of positive ints in a list
(define (numpos p) (if (null? p) 0 (+ (if (< (car p) 0) 0 1) (numpos (cdr p))))
- procedure that returns the maximum positive int in a list
(define (posmax p) (if (null? p) 0 (max (car p) (posmax (cdr p))))
(list of int), int –> bool
- procedure that returns #t when a value is an element of a list
(define (contains p e) (if (null? p) #f (or (= e (car p)) (contains (cdr p) e)))