This pattern is like the one for lists, except that it calls itself twice for interior nodes. This is essentially the same as the divide-and-conquer design pattern.
(defun myfun (tree)
         (if (interior? tree)
                 (combine (myfun (first tree)) ; left
                         (myfun (rest tree))) ; right
                 (baseanswer tree) )) ; leaf node
(defun addnums (tree) ; sum all numbers in tree (if (consp tree) (+ (addnums (first tree)) (addnums (rest tree)) ) (if (numberp tree) tree 0) ) ) public static Integer addnums (Object tree) { if ( consp(tree) ) return ( addnums(first((Cons)tree)) + addnums(rest((Cons)tree)) ); else if ( tree instanceof Integer ) return (Integer) tree; else return Integer.valueOf(0); }
Contents    Page-10    Prev    Next    Page+10    Index