Instantiating Design Patterns

sublis can be used to instantiate design patterns. For example, we can instantiate a tree-recursive accumulator pattern to make various functions:


(setq pattern
  '(defun ?fun (tree)
     (if (consp tree)
         (?combine (?fun (first tree))
                   (?fun (rest tree)))
         (if (?test tree) ?trueval ?falseval))))

>(sublis '((?fun      nnums  )
           (?combine  +      )
           (?test     numberp)
           (?trueval  1      )
           (?falseval 0      ))      pattern)

(DEFUN NNUMS (TREE)
  (IF (CONSP TREE)
      (+ (NNUMS (FIRST TREE))
         (NNUMS (REST TREE)))
      (IF (NUMBERP TREE) 1 0)))

>(nnums '(+ 3 (* i 5)))
2

Contents    Page-10    Prev    Next    Page+10    Index