CSC151 2007S, Class 41: Deep Recursion * I will continue to reserve time at the start of class for discussion of campus issues. Admin: * We will have visitors in class today and tomorrow. * Are there any final questions on Assignment 9? * EC for * Gene Gaub's concert Thursday at 11 a.m. in Herrick. * Thursday afternoon's CS Thursday extra on TCDB. * Lunch on Saturday from Neuroscience SEPC on "Why and how to go to grad school in Neuroscience" * 20th: Symphonic Band. Check the memo for time/place. * 22nd: Discussion of Mental Health on Campus * Reading for tomorrow: Association Lists. * Slight rearrangement to schedule. Friday is now "Higher-Order Procedures" Overview: * Lists, revisited. * Trees, introduced. * Deep recursion, considered. * Playing with color trees. * Lab! ==Lists== * A slow-indexable construction * Created with cons * A collection of values * The simplest list: Null * Add a value to the front of a list with cons * Also build them with (list ....), (append ...) How Mathematicians might describe a list * The empty list (null) is a list. * If we have a list, L, the result of prepending X onto L is a list. ==More on Cons== * Is everything you build with cons a list? NO! (cons null 'a) is not a list * So what do we call these structures built from cons cells that aren't lists? TREES * And why would we have them? To represent images TO confuse students To give us an exercise in recursion ==How Might we Write Procedures to Process Trees== * We will still have to recurse. * But we may need to recurse in TWO directions, rather than one. * Since it goes across and down, we call it DEEP recursion Original pattern of recursion (define procedure (lambda (val) (if (base-case-test) base-case (procedure (simplify val))))) (define tree-procedure (lambda (tree) (if (not (pair? tree)) base-case (combine (tree-procedure (car tree)) (tree-procedure (cdr tree)))))) ; Assume we've written (is-blue? color) (define ctree-contains-blue? (lambda (tree) (if (not (pair? tree)) ; Whee! We've finally hit a color. Now, we're supposed to ; return true if this subtree contains the color blue. (is-blue? tree) (or (ctree-contains-blue? (car tree)) (ctree-contains-blue? (cdr tree)))))) ; What if we wanted to count the number of blues? (define ctree-count-blue (lambda (tree) (if (not (pair? tree)) ; Whee! We've finally hit a color. Now, we're supposed to ; return true if this subtree contains the color blue. (if (is-blue? tree) 1 0) (+ (ctree-count-blue (car tree)) (ctree-count-blue (cdr tree))))))