week11 tweaks
[lambda.git] / week11.mdwn
index 1b38e49..a7558ff 100644 (file)
@@ -28,7 +28,9 @@ This searches for the `n`th element of a list that satisfies the predicate `test
                        | x :: xs -> helper x n xs
                in helper default n lst;;
 
-This duplicates a lot of the structure of `find_nth`; it just has enough different code to retrieve different information when the matching element is found. But now what if you wanted to retrieve yet a different kind of information...? Ideally, there should be some way to factor out the code to find the target element---the `n`th element of the list satisfying the predicate `test`---from the code that retrieves the information you want once the target is found. We might build upon the initial `find_nth` function, since that returns the *position* of the matching element. We could hand that result off to some other function that's designed to retrieve information of a specific sort surrounding that position. But suppose our list has millions of elements, and the target element is at position 600512. The search function will already have traversed 600512 elements of the list looking for the target, then the retrieval function would have to *start again from the beginning* and traverse those same 600512 elements again. It could go a bit faster, since it doesn't have to check each element against `test` as it traverses. It already knows how far it has to travel. But still, this should seem a bit wasteful.
+This duplicates a lot of the structure of `find_nth`; it just has enough different code to retrieve different information when the matching element is found. But now what if you wanted to retrieve yet a different kind of information...?
+
+Ideally, there should be some way to factor out the code to find the target element---the `n`th element of the list satisfying the predicate `test`---from the code that retrieves the information you want once the target is found. We might build upon the initial `find_nth` function, since that returns the *position* of the matching element. We could hand that result off to some other function that's designed to retrieve information of a specific sort surrounding that position. But suppose our list has millions of elements, and the target element is at position 600512. The search function will already have traversed 600512 elements of the list looking for the target, then the retrieval function would have to *start again from the beginning* and traverse those same 600512 elements again. It could go a bit faster, since it doesn't have to check each element against `test` as it traverses. It already knows how far it has to travel. But still, this should seem a bit wasteful.
 
 Here's an idea. What if we had some way of representing a list as "broken" at a specific point. For example, if our base list is:
 
@@ -64,7 +66,23 @@ The kind of data structure we're looking for here is called a **list zipper**. T
 
 would be represented as `([30; 20; 10], [40; 50; 60; 70; 80; 90])`. To move forward in the base list, we pop the head element `40` off of the head element of the second list in the zipper, and push it onto the first list, getting `([40; 30; 20; 10], [50; 60; 70; 80; 90])`. To move backwards again, we pop off of the first list, and push it onto the second. To reconstruct the base list, we just "move backwards" until the first list is empty. (This is supposed to evoke the image of zipping up a zipper; hence the data structure's name.)
 
-This is a very handy datastructure, and it will become even more handy when we translate it over to more complicated base structures, like trees. To help get a good conceptual grip on how to do that, it's useful to introduce a kind of symbolism for talking about zippers. This is just a metalanguage notation, for us theorists; we don't need our programs to interpret the notation. We'll use a specification like this:
+We had some discussio in seminar of the right way to understand the "zipper" metaphor. I think it's best to think of the tab of the zipper being here:
+
+                t
+                 a
+                  b
+                   40;
+               30;     50;
+           20;             60;
+       [10;                    70;
+                                   80;
+                                       90]
+
+And imagine that you're just seeing the left half of a real-zipper, rotated 60 degrees counter-clockwise. When the list is all "zipped up", we've "move backwards" to the state where the first element is targetted:
+
+       ([], [10; 20; 30; 40; 50; 60; 70; 80; 90])
+
+However you understand the "zipper" metaphor, this is a very handy datastructure, and it will become even more handy when we translate it over to more complicated base structures, like trees. To help get a good conceptual grip on how to do that, it's useful to introduce a kind of symbolism for talking about zippers. This is just a metalanguage notation, for us theorists; we don't need our programs to interpret the notation. We'll use a specification like this:
 
        [10; 20; 30; *; 50; 60; 70; 80; 90], * filled by 40
 
@@ -75,7 +93,7 @@ to represent a list zipper where the break is at position 3, and the element occ
 
 Now how could we translate a zipper-like structure over to trees? What we're aiming for is a way to keep track of where we are in a tree, in the same way that the "broken" lists let us keep track of where we are in the base list. In a particular application, you may only need to implement this for binary trees---trees where internal nodes always have exactly two subtrees. But to get the guiding idea, it's helpful first to think about trees that permit nodes to have many subtrees. Suppose we have the following tree:
 
-                                1000
+                                9200
                            /      |  \
                         /         |    \
                      /            |      \
@@ -100,9 +118,9 @@ Similarly for `node 50` and `node 80`. We haven't said yet what goes in the `par
 
        {parent = ...; siblings = [*; node 920; node 950]}, * filled by node 500
 
-And the parent of that targetted subtree should intuitively be a tree targetted on `node 1000`:
+And the parent of that targetted subtree should intuitively be a tree targetted on `node 9200`:
 
-       {parent = None; siblings = [*]}, * filled by node 1000
+       {parent = None; siblings = [*]}, * filled by node 9200
 
 This tree has no parents because it's the root of the base tree. Fully spelled out, then, our tree targetted on `node 50` would be:
 
@@ -111,20 +129,20 @@ This tree has no parents because it's the root of the base tree. Fully spelled o
              parent = {
                 parent = None;
                 siblings = [*]
-             }, * filled by node 1000;
+             }, * filled by node 9200;
              siblings = [*; node 920; node 950]
           }, * filled by node 500;
           siblings = [node 20; *; node 80]
        }, * filled by node 50
 
-In fact, there's some redundancy in this structure, at the points where we have `* filled by node 1000` and `* filled by node 500`. Most of `node 1000`---with the exception of any label attached to node `1000` itself---is determined by the rest of this structure; and so too with `node 500`. So we could really work with:
+In fact, there's some redundancy in this structure, at the points where we have `* filled by node 9200` and `* filled by node 500`. Most of `node 9200`---with the exception of any label attached to node `9200` itself---is determined by the rest of this structure; and so too with `node 500`. So we could really work with:
 
        {
           parent = {
              parent = {
                 parent = None;
                 siblings = [*]
-             }, label for * position (at node 1000);
+             }, label for * position (at node 9200);
              siblings = [*; node 920; node 950]
           }, label for * position (at node 500);
           siblings = [node 20; *; node 80]
@@ -208,7 +226,7 @@ where `node 500` refers to a tree built from a root node whose children are give
        {
           parent = None;
           siblings = [*]
-       }, * filled by node 1000
+       }, * filled by node 9200
 
 where the targetted element is the root of our base tree. Like the "moving backward" operation for the list zipper, this "moving upward" operation is supposed to be reminiscent of closing a zipper, and that's why these data structures are called zippers.