(module mp1-soln (lib "eopl.ss" "eopl") (provide subst) ;;;;;;;; Solution ;;;;;;;;;;;;;; ;; data definitions: ;; slist ::= () OR (cons sexp slist) -- ie, (list-of sexp). ;; sexp = symbol OR slist ;; subst : slist * symbol * slist -> slist ;; usage : (subst new old slist) ;; produces : a copy of slist with all occurrences of old ;; replaced by new. (define subst (lambda (new old slist) (cond [(null? slist) ; is the list empty? '()] ; yes: nothing to substitute [(pair? slist) ; no: work on the pieces (cons (subst-in-sexp new old (car slist)) (subst new old (cdr slist)))]))) ;; subst-in-s-exp : sym * sym * s-exp -> s-exp ;; usage: (subst-in-sexp new old sexp) ;; produces: a copy of sexp with all occurrences of old replaced by new (define subst-in-sexp (lambda (new old sexp) (if (symbol? sexp) (if (eqv? sexp old) new sexp) (subst new old sexp)))) )