t3x.org / nss / replace.html

(Nils' Scheme Snippets)

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

(replace formold formnew pair) => pair

 
Purpose
Replace elements of a pair.
 
Arguments
formold element to replace
formnew element to insert in the place of formold
 
Example
(replace 'x 'y '(lambda (x) x)) => (lambda (y) y)
(define (replace old new form)
  (cond
    ((equal? form old) new)
    ((pair? form)
      (cons (replace old new (car form))
            (replace old new (cdr form))))
    (else form)))

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