Presentation is loading. Please wait.

Presentation is loading. Please wait.

6.001 SICP 1 6.001 SICP – October 7 6001-Trees Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:

Similar presentations


Presentation on theme: "6.001 SICP 1 6.001 SICP – October 7 6001-Trees Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:"— Presentation transcript:

1 6.001 SICP 1 6.001 SICP – October 7 6001-Trees Trevor Darrell trevor@csail.mit.edu 32-D512 Office Hour: W 11 6.001 web page: http://sicp.csail.mit.edu/ section web page: http://www.csail.mit.edu/~trevor/6001/ Trees Re-implement pairs?

2 6.001 SICP 2 Trees Type Definition: Tree = List > | Leaf Leaf = C Example Operations : leaf? enum-leaves, map-tree, tree-ref,... 2 4 6 8 2 68 4 root children or subtrees

3 6.001 SICP 3 Example: enumerate leaves Flatten tree to list of leaves: (enumerate-leaves (list 1 (list 3 4 5)(list 2 30))) ==> (1 3 4 5 2 30) (define (e-l x) (cond ((null? x) nil) ((not (pair? x)) (list x)) (else (append (e-l (car x)) (e-l (cdr x))))))

4 6.001 SICP 4 Example: map-tree Write a procedure, (map-tree op tree), So if tree has value (((1 2) 3) (4 (5 6)) 7 (8 9 10)) then (map-tree inc tree) has value (((2 3) 4) (5 (6 7)) 8 (9 10 11)) You can use leaf? and map.

5 6.001 SICP 5 map-tree (define map-tree (lambda (op tree) (if (leaf? tree) (op tree) (map ??? tree))))

6 6.001 SICP 6 map-tree (define map-tree (lambda (op tree) (if (leaf? tree) (op tree) (map (lambda (subtree) (map-tree op subtree)) tree))))

7 6.001 SICP 7 tree-ref Given (((1 2) 3) (4 (5 6)) 7 (8 9 10)) To select the element 9 out of it, we use something like (second (fourth tree)) Instead let’s define tree-ref so that we can use (tree-ref (list 3 1) tree)

8 6.001 SICP 8 tree-ref (define tree-ref (lambda (index tree) (if (null? index) tree (tree-ref (cdr index) (list-ref tree (car index))))))

9 6.001 SICP 9 Example: deep-reverse Reverse the order of a tree: (deep-reverse (list 1 (list 2 3) (list 4 5 6))) ==> ((6 5 4) (3 2) 1) (define (reverse lst) (if (null? lst) nil (append (reverse (cdr lst)) (list (car lst)))) (define (deep-reverse tree) (if (not (pair? tree)) tree (map deep-reverse (reverse tree))) )

10 6.001 SICP 10 General tree-manip (define (tree-manip tree init leaf first rest accum) (cond ((null? tree) init) ((not (pair? tree)) (leaf tree)) (else (accum (tree-manip (first tree) init leaf first rest accum) (tree-manip (rest tree) init leaf first rest accum))))) Given : (define test-tree (list 1 (list 2 (list 3 (list 4) 5) 6) 7)) You can write expressions using tree-manip on test-tree will subsume many of the specific function we’ve just written, e.g.,:  Flatten a tree  Deep-reverse a tree  Sum up the values of the leaves of the tree  Take the product of the even-valued leaves of the tree  Create a new tree, which keeps the odd-valued leaves of the original tree within the same tree structure, but completely removes even-valued leaves.

11 6.001 SICP 11 And now for something completely different…. define a new pair abstraction! The names and procedures cons, car and cdr just fell into a black hole—I can’t use them to define adjoin, first, and rest! But I can define my-cons, a HOP that does: (my-cons 1 2)  procedure ((my-cons 1 2) #t)  1 ((my-cons 1 2) #f)  2 (define my-cons (lambda (a b) (lambda (p) (if p a b)))) (define adjoin mycons) (define (first p) (p #t)) (define (rest p) (p #f)))

12 6.001 SICP 12 Remember… calendar…


Download ppt "6.001 SICP 1 6.001 SICP – October 7 6001-Trees Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:"

Similar presentations


Ads by Google