tweak advanced
[lambda.git] / miscellaneous_lambda_challenges_and_advanced_topics.mdwn
index f69335a..0342fc6 100644 (file)
@@ -17,7 +17,7 @@ can use.
        native `let rec` or `define`, then you can't use the fixed-point combinators
        `Y` or <code>&Theta;</code>. Expressions using them will have non-terminating
        reductions, with Scheme's eager/call-by-value strategy. There are other
-       fixed-point combinators you can use with Scheme (in the [[week3]] notes they
+       fixed-point combinators you can use with Scheme (in the [week 3 notes](/week3/#index7h2) they
        were <code>Y&prime;</code> and <code>&Theta;&prime;</code>. But even with
        them, evaluation order still matters: for some (admittedly unusual)
        evaluation strategies, expressions using them will also be non-terminating.
@@ -60,18 +60,18 @@ can use.
        current list, the tail of the current list, and the result of continuing to
        fold `f` over the tail, with a given base value `z`.
 
-       Call this a **version 4** list. The empty list could be the same:
+       Call this a **version 4** list. The empty list can be the same as in v3:
 
-               empty === \f z. z
+       <pre><code>empty &equiv; \f z. z</code></pre>
 
        The list constructor would be:
 
-               make_list === \h t. \f z. f h t (t f z)
+       <pre><code>make_list &equiv; \h t. \f z. f h t (t f z)</code></pre>
 
        It differs from the version 3 `make_list` only in adding the extra argument
        `t` to the new, outer application of `f`.
 
-       Similarly, 5 as a v3 or Church numeral looks like this:
+       Similarly, `five` as a v3 or Church numeral looks like this:
 
                \s z. s (s (s (s (s z))))
 
@@ -117,7 +117,7 @@ can use.
        of a new list with the added member prepended to the old list. That is:
 
                let empty_set = empty  in
-               ; see the library for definition of any
+               ; see the library for definitions of any and eq
                let make_set = \new_member old_set. any (eq new_member) old_set
                                                        ; if any element in old_set was eq new_member
                                                        old_set
@@ -157,8 +157,8 @@ can use.
        d)`.)
 
        So, if we were searching the list that implements some set to see if the number
-       5 belonged to it, once we get to elements in the list that are larger than 5,
-       we can stop. If we haven't found 5 already, we know it's not in the rest of the
+       `5` belonged to it, once we get to elements in the list that are larger than `5`,
+       we can stop. If we haven't found `5` already, we know it's not in the rest of the
        list either.
 
        This is an improvement, but it's still a "linear" search through the list.
@@ -223,7 +223,7 @@ can use.
        parts of the list that have head `4` and head `5`, too.
 
        We *can* avoid *some* unneccessary computation. The search function can detect
-       that the result we've accumulated so far during the fold is now true, so we
+       that the result we've accumulated so far during the fold is now `true`, so we
        don't need to bother comparing `4` or `5` to `3` for equality. That will simplify the
        computation to some degree, since as we said, numerical comparison in the
        system we're working in is moderately expensive.
@@ -236,7 +236,7 @@ can use.
        It would be better if there were some way to "abort" the list traversal. If,
        having found the element we're looking for (or having determined that the
        element isn't going to be found), we could just immediately stop traversing the
-       list with our answer. Continuations will turn out to let us do that.
+       list with our answer. **Continuations** will turn out to let us do that.
 
        We won't try yet to fully exploit the terrible power of continuations. But
        there's a way that we can gain their benefits here locally, without yet having
@@ -254,17 +254,22 @@ can use.
 
        to get the first element of the pair. Of course you can lift that if you want:
 
-               extract_1st === \pair. pair (\x y. x)
+       <pre><code>extract_fst &equiv; \pair. pair (\x y. x)</code></pre>
 
        but at a lower level, the pair is still accepting its handler as an argument,
        rather than the handler taking the pair as an argument. (The handler gets *the
        pair's elements*, not the pair itself, as arguments.)
 
+       >       *Terminology*: we'll try to use names of the form `get_foo` for handlers, and
+       names of the form `extract_foo` for lifted versions of them, that accept the
+       lists (or whatever data structure we're working with) as arguments. But we may
+       sometimes forget.
+
        The v2 implementation of lists followed a similar strategy:
 
                v2list (\h t. do_something_with_h_and_t) result_if_empty
 
-       If the v2list here is not empty, then this will reduce to the result of
+       If the `v2list` here is not empty, then this will reduce to the result of
        supplying the list's head and tail to the handler `(\h t.
        do_something_with_h_and_t)`.
 
@@ -297,20 +302,20 @@ can use.
 
        What if the way we implemented the search procedure looked something like this?
 
-       At a given stage in the search, we wouldn't just apply some function f to the
-       head at this stage and the result accumulated so farfrom folding the same
-       function (and a base value) to the tail at this stage. And then pass the result
-       of doing so leftward along the rest of the list.
+       At a given stage in the search, we wouldn't just apply some function `f` to the
+       head at this stage and the result accumulated so far (from folding the same
+       function, and a base value, to the tail at this stage)...and then pass the result
+       of that application to the embedding, more leftward computation.
 
-       We'd also give that function a "handler" that expected the result of the
-       current stage as an argument, and evaluated to passing that result leftwards
-       along the rest of the list.
+       We'd *instead* give `f` a "handler" that expects the result of the current
+       stage *as an argument*, and then evaluates to what you'd get by passing that
+       result leftwards up the list, as before. 
 
        Why would we do that, you say? Just more flamboyant lifting?
 
        Well, no, there's a real point here. If we give the function a "handler" that
-       encodes the normal continuation of the fold leftwards through the list. We can
-       give it another "handler" as well. We can also give it the underlined handler:
+       encodes the normal continuation of the fold leftwards through the list, we can
+       also give it other "handlers" too. For example, we can also give it the underlined handler:
 
 
                the_search (\search_result. larger_computation search_result other_arguments)
@@ -319,7 +324,7 @@ can use.
        This "handler" encodes the search's having finished, and delivering a final
        answer to whatever else you wanted your program to do with the result of the
        search. If you like, at any stage in the search you might just give an argument
-       to this handler, instead of giving an argument to the handler that continues
+       to *this* handler, instead of giving an argument to the handler that continues
        the list traversal leftwards. Semantically, this would amount to *aborting* the
        list traversal! (As we've said before, whether the rest of the list traversal
        really gets evaluated will depend on what evaluation order is in place. But
@@ -334,8 +339,8 @@ can use.
 
                f 3 <result of folding f and z over [2; 1]> <handler to continue folding leftwards> <handler to abort the traversal>
 
-       `f`'s job would be to check whether 3 matches the element we're searching for
-       (here also 3), and if it does, just evaluate to the result of passing `true` to
+       `f`'s job would be to check whether `3` matches the element we're searching for
+       (here also `3`), and if it does, just evaluate to the result of passing `true` to
        the abort handler. If it doesn't, then evaluate to the result of passing
        `false` to the continue-leftwards handler.
 
@@ -350,27 +355,28 @@ can use.
        of the list multiplied to, because that would affect the answer you passed
        along to the continue-leftwards handler.
 
-       A **version 5** list would encode this kind of fold operation over the list, in
+       A **version 5** list encodes the kind of fold operation we're envisaging here, in
        the same way that v3 (and v4) lists encoded the simpler fold operation.
        Roughly, the list `[5;4;3;2;1]` would look like this:
 
 
                \f z continue_leftwards_handler abort_handler.
-                       <fold f and z over [4; 3; 2; 1]>
+                       <fold f and z over [4;3;2;1]>
                        (\result_of_fold_over_4321. f 5 result_of_fold_over_4321  continue_leftwards_handler abort_handler)
                        abort_handler
 
+               ; or, expanding the fold over [4;3;2;1]:
 
                \f z continue_leftwards_handler abort_handler.
                        (\continue_leftwards_handler abort_handler.
-                               <fold f and z over [3; 2; 1]>
+                               <fold f and z over [3;2;1]>
                                (\result_of_fold_over_321. f 4 result_of_fold_over_321 continue_leftwards_handler abort_handler)
                                abort_handler
                        )
                        (\result_of_fold_over_4321. f 5 result_of_fold_over_4321  continue_leftwards_handler abort_handler)
                        abort_handler
 
-               and so on               
+               ; and so on             
                
        Remarks: the `larger_computation_handler` should be supplied as both the
        `continue_leftwards_handler` and the `abort_handler` for the leftmost