Unformatted text preview: This definition of leaf? should work no matter how we represent the ADT. The straightforward implementation is ;;;;; In file cs61a/lectures/2.2/tree1.scm (define maketree cons) (define datum car) (define children cdr) • Mapping over trees One thing we might want to do with a tree is create another tree, with the same shape as the original, but with each datum replaced by some function of the original. This is the tree equivalent of map for lists. ;;;;; In file cs61a/lectures/2.2/tree1.scm (define (treemap fn tree) (maketree (fn (datum tree)) (map (lambda (t) (treemap fn t)) (children tree) ))) This is a remarkably simple and elegant procedure, especially considering the versatility of the data structures it can handle (trees of many different sizes and shapes). It’s one of the more beautiful things you’ll see in the course, so spend some time appreciating it. Every tree node consists of a datum and some children. In the new tree, the datum corresponding to this node should be the result of applying...
View
Full
Document
This note was uploaded on 02/17/2010 for the course COMPUTER S 26275 taught by Professor Harvey,b during the Spring '10 term at Berkeley.
 Spring '10
 Harvey,B

Click to edit the document details