Using sublis to Write Code

sublis can be used to write code by instantiating design patterns. For example, we can instantiate a test-accumulate pattern to make various counting functions:


(define pattern
  '(define (?fun tree)
     (if (pair? tree)
         (?combine (?fun (car tree))
                   (?fun (cdr tree)))
         (if (?test? tree) ?trueval ?falseval))))

>(sublis '((?fun . nsymbols) (?combine . +) (?test? . symbol?) (?trueval . 1) (?falseval . 0)) pattern)

(define (nsymbols tree) (if (pair? tree) (+ (nsymbols (car tree)) (nsymbols (cdr tree))) (if (symbol? tree) 1 0)))

Contents    Page-10    Prev    Next    Page+10    Index