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