; File: substall.scm ; Title: Substitution of OLD with NEW in a list on all levels ; ; Ex: > (subst1 0 5 '( (5 3) 5 (6 (5)) 5)) ; ( (0 3) 0 (6 (0)) 0) ; ; >(subst2 'x 'a '(a b (a 5) a)) ; (x b (x 5) x) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;> (atom? () ) -> #t ; > (atom? () ) (define (atom? Obj) ; (not (pair? Obj))) ;;;;;;;;;;;;;;;;;;;;;;;;;; substitutes an atom on all levels (define (subst1 n v L) ; n=new v=old (vechio) L=list (cond ((atom? L) L) ; L is an atom or () then L ((equal? (car L) v) (cons n (subst1 n v (cdr L)))) (#t (cons (subst1 n v (car L)) (subst1 n v (cdr L)))) )) ;;;;;;;;;;;;;;;;;;;;;;;;; Substitutes all occurrences of v on all levels (define subst2 (lambda ( n v L) ; n=new v=old L=list (map (lambda (z)(cond (( null? z) z) ((atom? z) (if (equal? z v) n z)) (#t (subst2 n v z)) )) L) ))