t3x.org / nss / substitute.html

(Nils' Scheme Snippets)

 
Paren matching: OFF  |  Category: lists  |  Overview  |  Scheme Books  |  License
 

(substitute form alist) => form

 
Purpose
Substitute subforms of a given form. The association list alist contains the forms to be substituted as keys and the substitutes as values.
 
Arguments
form source form
alist substitutions
 
Example
(substitute '(* (+ 5 7) 9) '(((+ 5 7) . 12)))
=> (* 12 9)
(define (substitute x a)
  (cond ((assoc x a) => cdr)
        ((pair? x)
          (cons (substitute (car x) a)
                (substitute (cdr x) a)))
        (else x)))

Copyright (C) 2007 Nils M Holm <nmh @ t3x . org>