5a2780d8459e95e45fa07ea0de4ce26c26a88a76
[lambda.git] / assignment8.mdwn
1 1.      Complete the definitions of `move_botleft` and `move_right_or_up` from the same-fringe solution in the [[week11]] notes. **Test your attempts** against some example trees to see if the resulting `make_fringe_enumerator` and `same_fringe` functions work as expected. Show us some of your tests.
2
3                 type 'a tree = Leaf of 'a | Node of ('a tree * 'a tree)
4
5                 type 'a starred_level = Root | Starring_Left of 'a starred_nonroot | Starring_Right of 'a starred_nonroot
6                 and 'a starred_nonroot = { parent : 'a starred_level; sibling: 'a tree };;
7
8                 type 'a zipper = { level : 'a starred_level; filler: 'a tree };;
9
10                 let rec move_botleft (z : 'a zipper) : 'a zipper =
11                         (* returns z if the targetted node in z has no children *)
12                         (* else returns move_botleft (zipper which results from moving down from z to the leftmost child) *)
13                         _____
14                         (* YOU SUPPLY THE DEFINITION *)
15
16
17                 let rec move_right_or_up (z : 'a zipper) : 'a zipper option =
18                         (* if it's possible to move right in z, returns Some (the result of doing so) *)
19                         (* else if it's not possible to move any further up in z, returns None *)
20                         (* else returns move_right_or_up (result of moving up in z) *)
21                         _____
22                         (* YOU SUPPLY THE DEFINITION *)
23
24
25                 let new_zipper (t : 'a tree) : 'a zipper =
26                         {level = Root; filler = t}
27                         ;;
28
29          
30
31                 let make_fringe_enumerator (t: 'a tree) =
32                         (* create a zipper targetting the root of t *)
33                         let zstart = new_zipper t
34                         in let zbotleft = move_botleft zstart
35                         (* create a refcell initially pointing to zbotleft *)
36                         in let zcell = ref (Some zbotleft)
37                         (* construct the next_leaf function *)
38                         in let next_leaf () : 'a option =
39                                 match !zcell with
40                                 | None -> (* we've finished enumerating the fringe *)
41                                         None
42                                 | Some z -> (
43                                         (* extract label of currently-targetted leaf *)
44                                         let Leaf current = z.filler
45                                         (* update zcell to point to next leaf, if there is one *)
46                                         in let () = zcell := match move_right_or_up z with
47                                                 | None -> None
48                                                 | Some z' -> Some (move_botleft z')
49                                         (* return saved label *)
50                                         in Some current
51                                 )
52                         (* return the next_leaf function *)
53                         in next_leaf
54                         ;;
55
56                 let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
57                         let next1 = make_fringe_enumerator t1
58                         in let next2 = make_fringe_enumerator t2
59                         in let rec loop () : bool =
60                                 match next1 (), next2 () with
61                                 | Some a, Some b when a = b -> loop ()
62                                 | None, None -> true
63                                 | _ -> false
64                         in loop ()
65                         ;;
66
67
68 2.      Here's another implementation of the same-fringe function, in Scheme. It's taken from <http://c2.com/cgi/wiki?SameFringeProblem>. It uses thunks to delay the evaluation of code that computes the tail of a list of a tree's fringe. It also involves passing continuations (`tailk`s) as arguments. Your assignment is to fill in the blanks in the code, **and also to supply comments to the code,** to explain what every significant piece is doing. Don't forget to supply the comments, this is an important part of the assignment.
69
70         This code uses Scheme's `cond` construct. That works like this;
71
72                 (cond
73                         ((test1 argument argument) result1)
74                         ((test2 argument argument) result2)
75                         ((test3 argument argument) result3)
76                         (else result4))
77
78         is equivalent to:
79
80                 (if (test1 argument argument)
81                         ; then
82                         result1
83                         ; else
84                         (if (test2 argument argument)
85                                 ; then
86                                 result2
87                                 ; else
88                                 (if (test3 argument argument)
89                                         ; then
90                                         result3
91                                         ; else
92                                         result4)))
93
94         Some other Scheme details:
95
96         *       `#t` is true and `#f` is false
97         *       `(lambda () ...)` constructs a thunk
98         *       there is no difference in meaning between `[...]` and `(...)`; we just sometimes use the square brackets for clarity
99         *       `'(1 . 2)` and `(cons 1 2)` are pairs (the same pair)
100         *       `(list)` and `'()` both evaluate to the empty list
101         *       `(null? lst)` tests whether `lst` is the empty list
102         *       non-empty lists are implemented as pairs whose second member is a list
103         *       `'()` `'(1)` `'(1 2)` `'(1 2 3)` are all lists
104         *       `(list)` `(list 1)` `(list 1 2)` `(list 1 2 3)` are the same lists as the preceding
105         *       `'(1 2 3)` and `(cons 1 '(2 3))` are both pairs and lists (the same list)
106         *       `(pair? lst)` tests whether `lst` is a pair; if `lst` is a non-empty list, it will also pass this test; if `lst` fails this test, it may be because `lst` is the empty list, or because it's not a list or pair at all
107         *       `(car lst)` extracts the first member of a pair / head of a list
108         *       `(cdr lst)` extracts the second member of a pair / tail of a list
109
110         Here is the implementation:
111
112                 (define (lazy-flatten tree)
113                   (letrec ([helper (lambda (tree tailk)
114                                   (cond
115                                     [(pair? tree)
116                                       (helper (car tree) (lambda () (helper _____ tailk)))]
117                                     [else (cons tree tailk)]))])
118                     (helper tree (lambda () _____))))
119                 
120                 (define (stream-equal? stream1 stream2)
121                   (cond
122                     [(and (null? stream1) (null? stream2)) _____]
123                     [(and (pair? stream1) (pair? stream2))
124                      (and (equal? (car stream1) (car stream2))
125                           _____)]
126                     [else #f]))
127                 
128                 (define (same-fringe? tree1 tree2)
129                   (stream-equal? (lazy-flatten tree1) (lazy-flatten tree2)))
130                 
131                 (define tree1 '(((1 . 2) . (3 . 4)) . (5 . 6)))
132                 (define tree2 '(1 . (((2 . 3) . (4 . 5)) . 6)))
133                 
134                 (same-fringe? tree1 tree2)
135
136