; Examples from Lecture 3, Wed., Sept. 10, 2008 (load "tester.scm") (define ((compose f g) x) (f (g x))) (define (square x) (* x x)) (define (cube x) (* x x x)) (test ((compose square cube) 2) 64) (test ((compose cube square) 2) 64) (test ((compose square (lambda(x) (+ x 1))) 5) 36) (test ((compose (lambda(x) (+ x 1)) square) 5) 26) (define (factors n) (if (< n 2) () (let ((factor (lowest-factor n 2))) (cons factor (factors (/ n factor)))))) (define (lowest-factor n trial) (if (> (* trial trial) n) n (if (divides? trial n) trial (lowest-factor n (+ 1 trial))))) (define (divides? trial n) (= 0 (modulo n trial))) (test (factors 19) '(19)) (test (factors 24) '(2 2 2 3)) (test (factors 1273892073903) '(3 3 141543563767)) (define (multi-assoc x L) (if (null? L) () (if (equal? x (first (first L))) (cons (first L) (multi-assoc x (rest L))) (multi-assoc x (rest L))))) (test (multi-assoc 3 '((1 a) (2 b) (3 c) (1 d) (2 e) (3 f) (3 g))) '((3 c) (3 f) (3 g))) (define (keep P L) (if (null? L) () (if (P (first L)) (cons (first L) (keep P (rest L))) (keep P (rest L))))) (test (keep (lambda(x) (> x 5)) '(3 5 7 9 11 1 2 4 6)) '(7 9 11 6)) (define (drop-nth n L) (if (null? L) () (if (= n 0) (rest L) (cons (first L) (drop-nth (- n 1) (rest L)))))) (test (drop-nth 3 '(a b c d e f g)) '(a b c e f g)) (tester 'show)