; Additional Examples from Lecture 4, Mon., Sept. 15, 2008 (load "tester.scm") (define (insert-nth n x L) (if (= n 0) (cons x L) (if (null? L) (error "no nth position in list" n L) (cons (first L) (insert-nth (- n 1) x (rest L))))) ) (test (insert-nth 3 'x '(a b c e f g)) '(a b c x e f g)) ;(test (insert-nth 13 'x '(a b c e f g)) '(a b c x e f g)) ; tail-recursive summation of a list ; using a nested definition (define (sum L) (define (sum2 L Acc) (if (null? L) Acc (sum2 (rest L) (+ (first L) Acc)))) (sum2 L 0)) (test (sum '(1 2 3 4 5 6 7 8 9 10)) 55) ; tail-recursive version of factorial (define (fac n) (trfac n 1)) (define (trfac n acc) (if (< n 2) acc (trfac (- n 1) (* n acc)))) (test (fac 5) 120) (tester 'show)