blob: e9bc999a2200ee536615e6302dd08e1d4abed32c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
;;"tree.scm" Implementation of COMMON LISP tree functions for Scheme
;;; Author: Aubrey Jaffer
;;;
;;; This code is in the public domain.
;; Deep copy of the tree -- new one has all new pairs. (Called
;; tree-copy in Dybvig.)
(define (tree:copy-tree tree)
(if (pair? tree)
(cons (tree:copy-tree (car tree))
(tree:copy-tree (cdr tree)))
tree))
;; Substitute occurrences of old equal? to new in tree.
;; Similar to tree walks in SICP without the internal define.
(define (tree:subst new old tree . equ?)
(set! equ? (if (null? equ?) equal? (car equ?)))
(letrec ((walk (lambda (tree)
(cond ((equ? old tree) new)
((pair? tree)
(cons (walk (car tree))
(walk (cdr tree))))
(else tree)))))
(walk tree)))
;; The next 2 aren't in CL. (Names from Dybvig)
(define (tree:substq new old tree)
(tree:subst new old tree eq?))
(define (tree:substv new old tree)
(tree:subst new old tree eqv?))
(define copy-tree tree:copy-tree)
(define subst tree:subst)
(define substq tree:substq)
(define substv tree:substv)
|