(define subst (lambda (new old ls) ;(writeln new old ls) (cond ((null? ls) '()) ;None of the examples show what subst does when ;new and old are lists. We could throw an error. ;In spite of the fact that this section is on flat ;recursion, I chose to use equal? which recursively ;compares. (else (cond ((equal? (car ls) old) ;(writeln "1: " new old ls) (cons new (subst new old (cdr ls))) ) (else ;(writeln "2: " new old ls) (cons (car ls) (subst new old (cdr ls))) ) ) ) ) ) ) ;============== Unit Tests ====================== (load "test.scm") (load "writeln.scm") (define msg "subst") (test msg (subst 'z 'a '(a b a c a)) '(z b z c z)) (test msg (subst '0 '1 '(0 1 0 1)) '(0 0 0 0)) (test msg (subst 'dog 'cat '(my dog is fun)) '(my dog is fun)) (test msg (subst 'two 'one '()) '()) (test msg (subst '(a a) 'x '(x b x c x)) '((a a) b (a a) c (a a))) (test msg (subst 'x '(a a) '((a a) b (a a) c (a a))) '(x b x c x)) (test msg (subst 'x '(a b) '((a a) b (a a) c (a a))) '((a a) b (a a) c (a a)))