Contents    Page-10    Prev    Next    Page+10    Index   

Pattern Matching

Pattern matching is the inverse of substitution: it tests to see whether an input is an instance of a pattern, and if so, how it matches.


>(match '(go ?expletive yourself)
        '(go bleep yourself))

((?expletive bleep) (t t))

(match '(defn ?fun [tree]
          (if (cons? tree)
              (?combine (?fun (first tree))
                        (?fun (rest tree)))
              (if (?test tree) ?trueval ?falseval)))

       '(defn nnums [tree]
          (if (cons? tree)
              (+ (nnums (first tree))
                 (nnums (rest tree)))
              (if (number? tree) 1 0))) )

((?falseval 0) (?trueval 1) (?test number?)
 (?combine +) (?fun nnums) (t t))