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:
(def pattern '(defn ?fun [tree] (if (cons? tree) (?combine (?fun (first tree)) (?fun (rest tree))) (if (?test tree) ?trueval ?falseval)))) >(sublis '((?fun nnums ) (?combine + ) (?test number?) (?trueval 1 ) (?falseval 0 )) pattern) (defn nnums [tree] (if (cons? tree) (+ (nnums (first tree)) (nnums (rest tree))) (if (number? tree) 1 0))) >(nnums '(+ 3 (* i 5))) 2