ps08 - CS51 Assignment 8: Lazy Sunday Due: Tuesday, April...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS51 Assignment 8: Lazy Sunday Due: Tuesday, April 21st, 2009, 11:59PM Total Points: 55 (including 5 style points) 1 Lazy Trees (23 points total) Exercise 1. 23 points In lecture, you learned about creating lazy lists (also called streams) using define-syntax-rule to delay evaluation. Here, we will ask you to write a lazy tree using a similar technique. To help you, weve defined a struct lbranch , which contains a value ( val ), a left child ( left ) and a right child ( right ). For testing, weve also provided the function (print-n-levels n sometree) , which prints the first n levels of sometree on a single line. (a) [10 points] Write the functions make-lt , lt-left , lt-right . lt-left and lt-right should re- turn the left and right children, respectively, of a lazy tree. (make-lt val left right) should return an lbranch struct with lbranch-val set to val , and with lbranch-left and lbranch-right set to unevaluated functions wrapping left and right , respectively. For example, consider the following definition: > ( define tree1 ( make-lt 1 tree1 tree1 )) > tree1 # < lbranch > > ( lt-val tree1 ) 1 > ( lt-left tree1 ) # < lbranch > > ( lbranch-left tree1 ) # < procedure : f > > ( lt-val ( lt-left tree1 )) 1 1 1 LAZY TREES CS51 Assignment 8: Lazy Sunday > ( print-n-levels 1 tree1 ) 1(1.1) This represents a tree with every val set to 1 . We have included the call to lbranch-left only for clarification. You should instead call lt-left whenever possible. (b) [5 points] Write ltmap , a map function for lazy trees. This takes a function and an lbranch representing the root of a lazy tree, and returns a new lazy tree with the function applied to every element of the original. For example: > ( define tree2 ( ltmap incr tree1 )) > ( print-n-levels 1 tree2 ) 2(2.2) (c) [5 points] As you may have noticed, we made our lbranch struct mutable. We can call (set-lbranch-left! parenttree childtree) to set the left child of parenttree to childtree . set-lbranch-right! and set-lbranch-val! work similarly. Mutable branches allow us to implement memoization to avoid the repeated computation of values, in a similar manner to the lazy list memoization described in lecture (See lecture notes 16 for more details)....
View Full Document

Page1 / 5

ps08 - CS51 Assignment 8: Lazy Sunday Due: Tuesday, April...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online