1)
(define (multiply-list list-of-nums)
(cond [(null? list-of-nums) 1]
[else (* (car list-of-nums) (multiply-list (cdr list-of-nums)))]))
2)
(define (sum-list list-of-nums)
(cond [(null? list-of-nums) 0]
[else (+ (car list-of-nums) (sum-list (cdr list-of-nums)))]))
3)
(define (average list-of-nums)
(avg-helper list-of-nums 0 0))
(define (avg-helper list-of-nums dividend divisor)
(cond [(null? list-of-nums) (* 1.0 (/ dividend divisor))]
[else (avg-helper (cdr list-of-nums) (+ dividend (car list-of-nums))
(+ divisor 1))]))
4)
(define (average-non-neg list-of-nums)
(avg-non-neg-helper list-of-nums 0 0))
(define (avg-non-neg-helper list-of-nums dividend divisor)
(cond [(null? list-of-nums) (* 1.0 (/ dividend divisor))]
[(>= (car list-of-nums) 0)
(avg-non-neg-helper (cdr list-of-nums)
(+ dividend (car list-of-nums))
(+ divisor 1))]
[else
(avg-non-neg-helper (cdr list-of-nums) dividend divisor)]))
5)
(define (count-items inlist) ;; aka length
(cond [(null? inlist) 0]
[else (+ 1 (count-items (cdr inlist)))]))
6)
(define (count-zeros list-of-nums)
(cond [(null? list-of-nums) 0]
[(equal? 0 (car list-of-nums)) (+ 1 (count-zeros (cdr list-of-nums)))]
[else (count-zeros (cdr list-of-nums))]))
7)
(define (largest list-of-nums)
(largest-helper (cdr list-of-nums) (car list-of-nums)))
(define (largest-helper list-of-nums max)
(cond [(null? list-of-nums) max]
[(> (car list-of-nums) max)
(largest-helper (cdr list-of-nums) (car list-of-nums))]
[else (largest-helper (cdr list-of-nums) max)]))
8)
(define (my-and inlist)
(cond [(null? inlist) #t]
[(car inlist) (my-and (cdr inlist))]
[else #f]))
9)
(define (my-or inlist)
(cond [(null? inlist) #f]
[(car inlist) #t]
[else (my-or (cdr inlist))]))
10)
(define (find-sqrts list-of-nums)
(cond [(null? list-of-nums) ()]
[else (cons (sqrt (car list-of-nums))
(find-sqrts (cdr list-of-nums)))]))
11)
(define (add-3 list-of-nums)
(cond [(null? list-of-nums) ()]
[else (cons (+ 3 (car list-of-nums))
(add-3 (cdr list-of-nums)))]))
12)
(define (delete-all-zeros list-of-nums)
(cond [(null? list-of-nums) ()]
[(= (car list-of-nums) 0) (delete-all-zeros (cdr list-of-nums))]
[else (cons (car list-of-nums)
(delete-all-zeros (cdr list-of-nums)))]))
13)
(define (collect-non-neg list-of-nums)
(cond [(null? list-of-nums) ()]
[(>= (car list-of-nums) 0)
(cons (car list-of-nums) (collect-non-neg (cdr list-of-nums)))]
[else (collect-non-neg (cdr list-of-nums))]))
14)
(define (swap-every-pair inlist)
(cond [(null? inlist) ()]
[(not (pair? (cdr inlist))) inlist] ; <-needed to handle an odd
[else (cons (cadr inlist) ; number of items in list
(cons (car inlist)
(swap-every-pair (cddr inlist))))]))
15)
(define (stutter inlist)
(cond [(null? inlist) ()]
[else (cons (car inlist)
(cons (car inlist)
(stutter (cdr inlist))))]))
16)
(define (make-list n item)
(cond [(= n 0) ()]
[else (cons item (make-list (- n 1) item))]))
17)
(define (intersection list1 list2)
(cond [(null? list1) ()]
[(member (car list1) list2)
(cons (car list1) (intersection (cdr list1) list2))]
[else (intersection (cdr list1) list2)]))
18)
(define (union list1 list2)
(cond [(null? list1) list2]
[(member (car list1) list2)
(union (cdr list1) list2)]
[else (cons (car list1) (union (cdr list1) list2))]))
19)
(define (make-mapping list1 list2)
(cond [(null? list1) ()]
[else (cons (list (car list1) (car list2))
(make-mapping (cdr list1) (cdr list2)))]))
20)
(define (add-pairs list1 list2)
(cond [(null? list1) ()]
[else (cons (+ (car list1) (car list2))
(add-pairs (cdr list1) (cdr list2)))]))
Copyright (c) 2003 by Kurt Eiselt. All rights reserved, with
the exception of stuff that belongs to somebody else.
Last revised: September 16, 2003