Contents    Page-10    Prev    Next    Page+10    Index   

Instantiating Design Patterns

sublis can 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 (car tree))
                   (?fun (cdr tree)))
         (if (?test tree) ?trueval ?falseval))))

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

(DEFUN NNUMS (TREE)
  (IF (CONSP TREE)
      (+ (NNUMS (CAR TREE))
         (NNUMS (CDR TREE)))
      (IF (NUMBERP TREE) 1 0)))

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