zipper -> week11
authorJim Pryor <profjim@jimpryor.net>
Sat, 27 Nov 2010 01:41:06 +0000 (20:41 -0500)
committerJim Pryor <profjim@jimpryor.net>
Sat, 27 Nov 2010 01:41:06 +0000 (20:41 -0500)
Signed-off-by: Jim Pryor <profjim@jimpryor.net>
assignment8.mdwn [new file with mode: 0644]
index.mdwn
new_stuff.mdwn
week11.mdwn [moved from zipper.mdwn with 100% similarity]

diff --git a/assignment8.mdwn b/assignment8.mdwn
new file mode 100644 (file)
index 0000000..547db8e
--- /dev/null
@@ -0,0 +1,61 @@
+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.
+
+               type 'a tree = Leaf of 'a | Node of ('a tree * 'a tree)
+
+               type 'a starred_tree = Root | Starring_Left of 'a starred_pair | Starring_Right of 'a starred_pair
+               and 'a starred_pair = { parent : 'a starred_tree; sibling: 'a tree }
+               and 'a zipper = { tree : 'a starred_tree; filler: 'a tree };;
+
+               let rec move_botleft (z : 'a zipper) : 'a zipper =
+                       (* returns z if the targetted node in z has no children *)
+                       (* else returns move_botleft (zipper which results from moving down and left in z) *)
+                       YOU SUPPLY THE DEFINITION
+
+
+               let rec move_right_or_up (z : 'a zipper) : 'a zipper option =
+                       (* if it's possible to move right in z, returns Some (the result of doing so) *)
+                       (* else if it's not possible to move any further up in z, returns None *)
+                       (* else returns move_right_or_up (result of moving up in z) *)
+                       YOU SUPPLY THE DEFINITION
+
+
+               let new_zipper (t : 'a tree) : 'a zipper =
+                       {tree = Root; filler = t}
+                       ;;
+
+               let make_fringe_enumerator (t: 'a tree) =
+                       (* create a zipper targetting the root of t *)
+                       let zstart = new_zipper t
+                       in let zbotleft = move_botleft zstart
+                       (* create a refcell initially pointing to zbotleft *)
+                       in let zcell = ref (Some zbotleft)
+                       (* construct the next_leaf function *)
+                       in let next_leaf () : 'a option =
+                               match !zcell with
+                               | None -> (* we've finished enumerating the fringe *)
+                                       None
+                               | Some z -> (
+                                       (* extract label of currently-targetted leaf *)
+                                       let Leaf current = z.filler
+                                       (* update zcell to point to next leaf, if there is one *)
+                                       in let () = zcell := match move_right_or_up z with
+                                               | None -> None
+                                               | Some z' -> Some (move_botleft z')
+                                       (* return saved label *)
+                                       in Some current
+                               )
+                       (* return the next_leaf function *)
+                       in next_leaf
+                       ;;
+
+               let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
+                       let next1 = make_fringe_enumerator t1
+                       in let next2 = make_fringe_enumerator t2
+                       in let rec loop () : bool =
+                               match next1 (), next2 () with
+                               | Some a, Some b when a = b -> loop ()
+                               | None, None -> true
+                               | _ -> false
+                       in loop ()
+                       ;;
+
index f4335f9..f746e61 100644 (file)
@@ -65,7 +65,9 @@ preloaded is available at [[assignment 3 evaluator]].
 
 >      Topics: Calculator Improvements, including mutation
 
-(30 Nov) Lecture notes for Week11
+(30 Nov) Lecture notes for Week11; Assignment8.
+
+>      Topics: Zippers, Continuations
 
 (6 Dec) Lecture notes for Week12
 
index fd0b7bf..8a74c05 100644 (file)
@@ -2,6 +2,6 @@ Page for Chris and Jim to see what each other is working on, but hasn't necessar
 
 [[Week10]]
 
-[[Zipper]]
+[[Week11]]
 
 
similarity index 100%
rename from zipper.mdwn
rename to week11.mdwn