X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=week11.mdwn;h=14d5eed2745a5ab8cb140fc76c09f477d93d47c6;hp=97416e2c1d5110145ac63ed8a9af67ed79f01393;hb=8fbf14566363197bb08307a6bf4daece2f27602e;hpb=63822483bf23bc42870790e40e4424d862d36cd5 diff --git a/week11.mdwn b/week11.mdwn index 97416e2c..14d5eed2 100644 --- a/week11.mdwn +++ b/week11.mdwn @@ -183,7 +183,7 @@ For simplicity, I'll continue to use the abbreviated form: {parent = ...; siblings = [subtree 20; *; subtree 80]}, * filled by subtree 50 -But that should be understood as standing for the more fully-spelled-out structure. Structures of this sort are called **tree zippers**, for a reason that will emerge. They should already seem intuitively similar to list zippers, though, at least in what we're using them to represent. I think it may initially be more helpful to call these **targetted trees**, though, and so will be switching back and forth between this different terms. +But that should be understood as standing for the more fully-spelled-out structure. Structures of this sort are called **tree zippers**. They should already seem intuitively similar to list zippers, at least in what we're using them to represent. I think it may also be helpful to call them **targetted trees**, though, and so will be switching back and forth between these different terms. Moving left in our targetted tree that's targetted on `node 50` would be a matter of shifting the `*` leftwards: @@ -235,7 +235,7 @@ Moving upwards yet again would get us: siblings = [*; subtree 920; subtree 950] }, * filled by subtree 500' -where `subtree 500'` refers to a tree built from a root node whose children are given by the list `[*; subtree 50; subtree 80]`, with `subtree 20` inserted into the `*` position. Moving upwards yet again would get us: +where `subtree 500'` refers to a tree built from a root node whose children are given by the list `[*; subtree 50; subtree 80]`, with `subtree 20'` inserted into the `*` position. Moving upwards yet again would get us: { parent = None; @@ -252,7 +252,7 @@ We haven't given you a real implementation of the tree zipper, but only a sugges ##Same-fringe using a tree zipper## -Recall back in [[Assignment4]], we asked you to enumerate the "fringe" of a leaf-labeled tree. Both of these trees: +Recall back in [[Assignment4]], we asked you to enumerate the "fringe" of a leaf-labeled tree. Both of these trees (here I *am* drawing the labels in the diagram): . . / \ / \ @@ -260,7 +260,7 @@ Recall back in [[Assignment4]], we asked you to enumerate the "fringe" of a leaf / \ / \ 1 2 2 3 -have the same fringe: `[1;2;3]`. We also asked you to write a function that determined when two trees have the same fringe. The way you approached that back then was to enumerate each tree's fringe, and then compare the two lists for equality. Today, and then again in a later class, we'll encounter two new ways to approach the problem of determining when two trees have the same fringe. +have the same fringe: `[1;2;3]`. We also asked you to write a function that determined when two trees have the same fringe. The way you approached that back then was to enumerate each tree's fringe, and then compare the two lists for equality. Today, and then again in a later class, we'll encounter new ways to approach the problem of determining when two trees have the same fringe. Supposing you did work out an implementation of the tree zipper, then one way to determine whether two trees have the same fringe would be: go downwards (and leftwards) in each tree as far as possible. Compare the targetted leaves. If they're different, stop because the trees have different fringes. If they're the same, then for each tree, move rightward if possible; if it's not (because you're at the rightmost position in a sibling list), more upwards then try again to move rightwards. Repeat until you are able to move rightwards. Once you do move rightwards, go downwards (and leftwards) as far as possible. Then you'll be targetted on the next leaf in the tree's fringe. The operations it takes to get to "the next leaf" may be different for the two trees. For example, in these trees: @@ -324,9 +324,10 @@ Here is how you can extract the components of a labeled record: Anyway, using record types, we might define the tree zipper interface like so: - 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 };; + 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 };; let rec move_botleft (z : 'a zipper) : 'a zipper = (* returns z if the targetted node in z has no children *)