;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Code from Wed Apr 14, 2004 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; generate a list of consecutive integers from start to end (define integers-from-to (lambda (start end) (if (< end start) '() (cons start (integers-from-to (+ start 1) end))))) ;; generate a geometric list of n numbers with ;; first element a, ;; and constant ratio r (define geom (lambda (a r n) (if (= n 1) (list a) (cons a (geom (* a r) r (- n 1)))))) ;; generate a list of k random numbers, where each number ;; in the list is a random integer between 0 to n-1 inclusive. (define rand-num-list (lambda (n k) (if (= k 0) '() (cons (random n) (rand-num-list n (- k 1)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Code from Wed Apr 16, 2004 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define sum (lambda (num-list) (if (null? num-list) 0 (+ (car num-list) (sum (cdr num-list)))))) (define infinity (/ 1.0 0.0)) (define minimum (lambda (num-list) (if (null? num-list) infinity (let ((min-of-rest (minimum (cdr num-list)))) (if (< (car num-list) min-of-rest) (car num-list) min-of-rest))))) ;; Implementing Scheme Library Procedures ;; The s in front of procedure names means they are our own version. (define snull? (lambda (x) (equal? x '()))) (define slist? (lambda (x) (or (snull? x) (and (pair? x) (slist? (cdr x)))))) (define slength (lambda (lst) (if (snull? lst) 0 (+ 1 (slength (cdr lst)))))) (define slist-ref (lambda (lst n) (if (= n 0) (car lst) (slist-ref (cdr lst) (- n 1))))) (define slist-tail (lambda (lst n) (if (= n 0) lst (slist-tail (cdr lst) (- n 1))))) (define sappend (lambda (lst1 lst2) (if (snull? lst1) lst2 (cons (car lst1) (sappend (cdr lst1) lst2)))))