#lang racket (require rackunit) (provide subst) ;; ------------------------------------------------------------------------- ;; PROBLEM 2: subst (first version: straight SR) ;; ------------------------------------------------------------------------- ;; (define (subst new old slist) ;; (if (null? slist) ;; ;; handle empty list ;; ;; handle list containing a symbol-expression ;; ))) ;; (define (subst new old slist) ;; (if (null? slist) ;; '() ;; ;; handle list containing a symbol-expression ;; ))) ;; (define (subst new old slist) ;; (if (null? slist) ;; '() ;; (if (symbol? (first slist)) ;; ;; handle a symbol in the first ;; ;; handle an slist in the first ;; )))) ;; (define (subst new old slist) ;; (if (null? slist) ;; '() ;; (if (symbol? (first slist)) ;; (if (eq? (first slist) old) ;; (cons new (subst new old (rest slist))) ;; (cons (first slist) (subst new old (rest slist)))) ;; ;; handle an slist in the first slot ;; )))) (define (subst new old slist) (if (null? slist) '() (if (symbol? (first slist)) (if (eq? (first slist) old) (cons new (subst new old (rest slist))) (cons (first slist) (subst new old (rest slist)))) (cons (subst new old (first slist)) (subst new old (rest slist))) ))) ;; ------------------------------------------------------------------------- (check-equal? (subst 'd 'b '(a b c a b c d)) '(a d c a d c d)) (check-equal? (subst 'a 'b '((a b) (((b g r) (f r)) c (d e)) b)) '((a a) (((a g r) (f r)) c (d e)) a)) ;; ------------------------------------------------------------------------- ;; This works. But it is complex and seems dissatisfying. ;; Look at all the duplication! We can do better... ;; if we admit that we didn't really follow the data. ;; -------------------------------------------------------------------------