2af6513622063c0231dbcc58ae82145952315b38
[lambda.git] / exercises / _assignment12.mdwn
1 1.  Complete the definitions of `move_botleft` and `move_right_or_up` from the same-fringe solution in [[this week's notes|/topics/week12_list_and_tree_zippers]]. **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 new_zipper (t : 'a tree) : 'a zipper =
11           {level = Root; filler = t}
12
13         let rec move_botleft (z : 'a zipper) : 'a zipper =
14           (* returns z if the targetted node in z has no children *)
15           (* else returns move_botleft (zipper which results from moving down from z to the leftmost child) *)
16           _____ (* YOU SUPPLY THE DEFINITION *)
17
18     <!--
19     match z.filler with Leaf _ -> z | Node (l, r) -> move_botleft { level = Starring_left { parent = z.level; sibling = r }; filler = l }
20     -->
21
22         let rec move_right_or_up (z : 'a zipper) : 'a zipper option =
23           (* if it's possible to move right in z, returns Some (the result of doing so) *)
24           (* else if it's not possible to move any further up in z, returns None *)
25           (* else returns move_right_or_up (result of moving up in z) *)
26           _____ (* YOU SUPPLY THE DEFINITION *)
27
28     <!--
29     match z.level with
30     | Starring_left { parent = p; sibling = right } -> Some { level = Starring_right { parent = p; sibling = z.filler }; filler = right }
31     | Starring_right { parent = p; sibling = left } -> let new_tree = Node (left, z.filler) in move_right_or_up { level = p; filler = new_tree}
32     | Root -> None
33     -->
34
35     &nbsp;
36
37         let make_fringe_enumerator (t: 'a tree) : 'b * 'a zipper option =
38           (* create a zipper targetting the botleft of t *)
39           let zbotleft = move_botleft (new_zipper t) in
40           (* create initial state, pointing to zbotleft *)
41           let initial_state = Some zbotleft in
42           (* construct the next_leaf function *)
43           let next_leaf : 'a zipper option -> ('a * 'a zipper option) option = function
44             | Some z -> (
45               (* extract label of currently-targetted leaf *)
46               let Leaf current = z.filler in
47               (* create next_state pointing to next leaf, if there is one *)
48               let next_state : 'a zipper option = match move_right_or_up z with
49                 | None -> None
50                 | Some z' -> Some (move_botleft z') in
51               (* return saved label and next_state *)
52               Some (current, next_state)
53               )
54             | None -> (* we've finished enumerating the fringe *)
55               None in
56           (* return the next_leaf function and initial state *)
57           next_leaf, initial_state
58
59         let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
60           let next1, initial_state1 = make_fringe_enumerator t1 in
61           let next2, initial_state2 = make_fringe_enumerator t2 in
62           let rec loop state1 state2 : bool =
63             match next1 state1, next2 state2 with
64             | Some (a, state1'), Some (b, state2') when a = b -> loop state1' state2'
65             | None, None -> true
66             | _ -> false in
67           loop initial_state1 initial_state2
68
69
70 2.  Now we'll talk about another way to implement the `make_fringe_enumerator` function above (and so too the `same_fringe` function which uses it). Notice that the pattern given above is that the `make_fringe_enumerator` creates a `process` function and an initial state, and each time you want to advance the `process` by one step, you do so by calling it with the current state. It will return a result plus a modified state, which you can use when you want to call it again and take another step. All of the `process` function's memory about where it is in the enumeration is contained in the state. If you saved an old state, took three steps, and then called the `process` function again with the saved old state, it would be back where it was three steps ago. But in fact, the way we use the process and state above, there is no back-tracking. Neither do we "fork" any of the states and pursue different forward paths. Their progress is deterministic, and fixed independently of anything that `same_fringe` might do. All that's up to `same_fringe` is to take the decision of when (and whether) to take another step forward.
71
72     Given that usage pattern, it would be appropriate and convenient to make the `process` function remember its state itself, in a mutable variable. The client function `same_fringe` doesn't need to do anything with, or even be given access to, this variable. Here's how we might write `make_fringe_enumerator` according to this plan:
73
74         let make_fringe_enumerator (t: 'a tree) =
75           (* create a zipper targetting the botleft of t *)
76           let zbotleft = move_botleft (new_zipper t) in
77           (* create refcell, initially pointing to zbotleft *)
78           let zcell = ref (Some zbotleft) in
79           (* construct the next_leaf function *)
80           let next_leaf () : 'a option =
81             match !zcell with
82             | Some z -> (
83               (* extract label of currently-targetted leaf *)
84               let Leaf current = z.filler in
85               (* update zcell to point to next leaf, if there is one *)
86               let () = zcell := match move_right_or_up z with
87                 | None -> None
88                 | Some z' -> _____ in
89               (* return saved label *)
90               _____
91               )
92             | None -> (* we've finished enumerating the fringe *)
93               None in
94           (* return the next_leaf function *)
95           next_leaf
96
97         let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
98           let next1 = make_fringe_enumerator t1 in
99           let next2 = make_fringe_enumerator t2 in
100           let rec loop () : bool =
101             match _____, _____ with
102             | Some a, Some b when a = b -> loop ()
103             | None, None -> true
104             | _ -> false in
105           loop ()
106
107     You should fill in the blanks.
108
109     <!--
110         let make_fringe_enumerator (t: 'a tree) =
111           (* create a zipper targetting the botleft of t *)
112           let zbotleft = move_botleft (new_zipper t) in
113           (* create refcell, initially pointing to zbotleft *)
114           let zcell = ref (Some zbotleft) in
115           (* construct the next_leaf function *)
116           let next_leaf () : 'a option =
117             match !zcell with
118             | Some z -> (
119               (* extract label of currently-targetted leaf *)
120               let Leaf current = z.filler in
121               (* update zcell to point to next leaf, if there is one *)
122               let () = zcell := match move_right_or_up z with
123                 | None -> None
124                 | Some z' -> Some (move_botleft z') in
125               (* return saved label *)
126               Some current
127               )
128             | None -> (* we've finished enumerating the fringe *)
129               None in
130           (* return the next_leaf function *)
131           next_leaf
132
133         let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
134           let next1 = make_fringe_enumerator t1 in
135           let next2 = make_fringe_enumerator t2 in
136           let rec loop () : bool =
137             match next1 (), next2 () with
138             | Some a, Some b when a = b -> loop ()
139             | None, None -> true
140             | _ -> false in
141           loop ()
142     -->
143
144
145 3.  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 "the rest of the enumeration of the fringe" as a thunk argument (`tail-thunk` below). 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.
146
147     This code uses Scheme's `cond` construct. That works like this;
148
149         (cond
150             ((test1 argument argument) result1)
151             ((test2 argument argument) result2)
152             ((test3 argument argument) result3)
153             (else result4))
154
155     is equivalent to:
156
157         (if (test1 argument argument)
158            ; then
159              result1
160            ; else
161              (if (test2 argument argument)
162                 ; then
163                   result2
164                 ; else
165                   (if (test3 argument argument)
166                      ; then
167                        result3
168                      ; else
169                        result4)))
170
171     Some other Scheme details or reminders:
172
173     *   `#t` is true and `#f` is false
174     *   `(lambda () ...)` constructs a thunk
175     *   there is no difference in meaning between `[...]` and `(...)`; we just sometimes use the square brackets for clarity
176     *   `'(1 . 2)` and `(cons 1 2)` are pairs (the same pair)
177     *   `(list)` and `'()` both evaluate to the empty list
178     *   `(null? lst)` tests whether `lst` is the empty list
179     *   non-empty lists are implemented as pairs whose second member is a list
180     *   `'()` `'(1)` `'(1 2)` `'(1 2 3)` are all lists
181     *   `(list)` `(list 1)` `(list 1 2)` `(list 1 2 3)` are the same lists as the preceding
182     *   `'(1 2 3)` and `(cons 1 '(2 3))` are both pairs and lists (the same list)
183     *   `(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
184     *   `(car lst)` extracts the first member of a pair / head of a list
185     *   `(cdr lst)` extracts the second member of a pair / tail of a list
186
187     Here is the implementation:
188
189         (define (lazy-flatten tree)
190           (letrec ([helper (lambda (tree tail-thunk)
191                              (cond
192                                [(pair? tree)
193                                  (helper (car tree) (lambda () (helper _____ tail-thunk)))]
194                                [else (cons tree tail-thunk)]))])
195                   (helper tree (lambda () _____))))
196
197         (define (stream-equal? stream1 stream2)
198           (cond
199             [(and (null? stream1) (null? stream2)) _____]
200             [(and (pair? stream1) (pair? stream2))
201               (and (equal? (car stream1) (car stream2))
202                 _____)]
203             [else #f]))
204
205         (define (same-fringe? tree1 tree2)
206           (stream-equal? (lazy-flatten tree1) (lazy-flatten tree2)))
207
208         (define tree1 '(((1 . 2) . (3 . 4)) . (5 . 6)))
209         (define tree2 '(1 . (((2 . 3) . (4 . 5)) . 6)))
210
211         (same-fringe? tree1 tree2)
212
213 The previous problem implemented a "stream" in Scheme. A stream is like a list in that it wraps a series of elements of a single type. It differs from a list in that the tail of the series is left uncomputed until needed. We will turn the stream on and off by thunking it. Here is one way to implement streams in OCaml:
214
215     type 'a stream = End | Next of 'a * (unit -> 'a stream);;
216
217 There is a special stream called End that represents a stream that contains no (more) elements, analogous to the empty list []. Streams that are not empty contain a first object, paired with a thunked stream representing the rest of the series. In order to get access to the next element in the stream, we must force the thunk by applying it to the unit. Watch the behavior of this stream in detail. This stream delivers the natural numbers, in order: 1, 2, 3, ...
218
219     # let rec make_int_stream i = Next (i, fun () -> make_int_stream (i + 1));;
220     val make_int_stream : int -> int stream = [fun]
221
222     # let int_stream = make_int_stream 1;;
223     val int_stream : int stream = Next (1, [fun])         (* First element: 1 *)
224
225     # let tail = match int_stream with Next (i, rest) -> rest;;      
226     val tail : unit -> int stream = [fun]                 (* Tail: a thunk *)
227
228     (* Force the thunk to compute the second element *)
229     # tail ();;
230     - : int stream = Next (2, [fun])                      (* Second element: 2 *)
231
232     # match tail () with Next (_, rest) -> rest ();;
233     - : int stream = Next (3, [fun])                      (* Third element: 3 *)
234
235 You can think of `int_stream` as a functional object that provides access to an infinite sequence of integers, one at a time. It's as if we had written `[1;2;...]` where `...` meant "continue for as long as some other process needs new integers".