X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=week11.mdwn;h=81534fcd5046108f232438adaa3ac13f9bd3a2d4;hp=14d5eed2745a5ab8cb140fc76c09f477d93d47c6;hb=3562cbe6843f487857521a01f5291a2e578a7ed9;hpb=8fbf14566363197bb08307a6bf4daece2f27602e diff --git a/week11.mdwn b/week11.mdwn index 14d5eed2..81534fcd 100644 --- a/week11.mdwn +++ b/week11.mdwn @@ -327,18 +327,18 @@ Anyway, using record types, we might define the tree zipper interface like so: type 'a starred_level = Root | Starring_Left of 'a starred_nonroot | Starring_Right of 'a starred_nonroot and 'a starred_nonroot = { parent : 'a starred_level; sibling: 'a tree };; - type 'a zipper = { tree : 'a starred_level; filler: 'a tree };; + type 'a zipper = { level : 'a starred_level; 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) *) @@ -349,12 +349,12 @@ Anyway, using record types, we might define the tree zipper interface like so: (* else returns move_right_or_up (result of moving up in z) *) @@ -362,22 +362,19 @@ Anyway, using record types, we might define the tree zipper interface like so: The following function takes an 'a tree and returns an 'a zipper focused on its root: let new_zipper (t : 'a tree) : 'a zipper = - {tree = Root; filler = t} + {level = Root; filler = t} ;; Finally, we can use a mutable reference cell to define a function that enumerates a tree's fringe until it's exhausted: 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 zipper targetting the botleft of t *) + let zbotleft = move_botleft (new_zipper t) (* 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 @@ -387,6 +384,8 @@ Finally, we can use a mutable reference cell to define a function that enumerate | Some z' -> Some (move_botleft z') (* return saved label *) in Some current + | None -> (* we've finished enumerating the fringe *) + None ) (* return the next_leaf function *) in next_leaf