This preview shows page 1. Sign up to view the full content.
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). Its one of the more beautiful things youll 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
 Spring '10
 Harvey,B

Click to edit the document details