lists-monad tweaks
[lambda.git] / from_lists_to_continuations.mdwn
index 5bc4986..d6eb685 100644 (file)
@@ -69,7 +69,7 @@ This is a task well-suited to using a zipper.  We'll define a function
 `tz` (for task with zippers), which accomplishes the task by mapping a
 `char list zipper` to a `char list`.  We'll call the two parts of the
 zipper `unzipped` and `zipped`; we start with a fully zipped list, and
-move elements to the zipped part by pulling the zipper down until the
+move elements to the unzipped part by pulling the zipper down until the
 entire list has been unzipped (and so the zipped half of the zipper is empty).
 
        type 'a list_zipper = ('a list) * ('a list);;
@@ -91,7 +91,7 @@ Task completed.
 
 One way to see exactly what is going on is to watch the zipper in
 action by tracing the execution of `tz`.  By using the `#trace`
-directive in the Ocaml interpreter, the system will print out the
+directive in the OCaml interpreter, the system will print out the
 arguments to `tz` each time it is (recurcively) called.  Note that the
 lines with left-facing arrows (`<--`) show (recursive) calls to `tz`,
 giving the value of its argument (a zipper), and the lines with
@@ -142,7 +142,7 @@ list) -> 'a'::('b'::tail)`.
 
 This means that we can now represent the unzipped part of our
 zipper---the part we've already unzipped---as a continuation: a function
-describing how to finish building the list.  We'll write a new
+describing how to finish building a list.  We'll write a new
 function, `tc` (for task with continuations), that will take an input
 list (not a zipper!) and a continuation and return a processed list.
 The structure and the behavior will follow that of `tz` above, with
@@ -158,13 +158,13 @@ some small but interesting differences.  We've included the orginal
        let rec tc (l: char list) (c: (char list) -> (char list)) =
            match l with
            | [] -> List.rev (c [])
-           | 'S'::zipped -> tc zipped (fun x -> c (c x))
-           | target::zipped -> tc zipped (fun x -> target::(c x));;
+           | 'S'::zipped -> tc zipped (fun tail -> c (c tail))
+           | target::zipped -> tc zipped (fun tail -> target::(c tail));;
        
-       # tc ['a'; 'b'; 'S'; 'd'] (fun x -> x);;
+       # tc ['a'; 'b'; 'S'; 'd'] (fun tail -> tail);;
        - : char list = ['a'; 'b'; 'a'; 'b']
        
-       # tc ['a'; 'S'; 'b'; 'S'] (fun x -> x);;
+       # tc ['a'; 'S'; 'b'; 'S'] (fun tail -> tail);;
        - : char list = ['a'; 'a'; 'b'; 'a'; 'a'; 'b']
 
 To emphasize the parallel, I've re-used the names `zipped` and
@@ -184,36 +184,33 @@ you can see this difference in the fact that in `tz`, we have to glue
 together the two instances of `unzipped` with an explicit (and
 relatively inefficient) `List.append`.
 In the `tc` version of the task, we simply compose `c` with itself:
-`c o c = fun x -> c (c x)`.
+`c o c = fun tail -> c (c tail)`.
 
-Why use the identity function as the initial continuation?  Well, if
-you have already constructed the initial list `"abSd"`, what's the next
-step in the recipe to produce the desired result, i.e, the very same
-list, `"abSd"`?  Clearly, the identity continuation.
+A call `tc ['a'; 'b'; 'S'; 'd']` yields a partially-applied function; it still waits for another argument, a continuation of type `char list -> char list`. We have to give it an "initial continuation" to get started. Here we supply *the identity function* as the initial continuation. Why did we choose that? Well, if
+you have already constructed the initial list `"abSd"`, what's the desired continuation? What's the next step in the recipe to produce the desired result, i.e, the very same list, `"abSd"`?  Clearly, the identity function.
 
 A good way to test your understanding is to figure out what the
 continuation function `c` must be at the point in the computation when
 `tc` is called with the first argument `"Sd"`.  Two choices: is it
-`fun x -> a::b::x`, or it is `fun x -> b::a::x`?  The way to see if
+`fun tail -> 'a'::'b'::tail`, or it is `fun tail -> 'b'::'a'::tail`?  The way to see if
 you're right is to execute the following command and see what happens:
 
-    tc ['S'; 'd'] (fun x -> 'a'::'b'::x);;
+    tc ['S'; 'd'] (fun tail -> 'a'::'b'::tail);;
 
 There are a number of interesting directions we can go with this task.
 The reason this task was chosen is because it can be viewed as a
 simplified picture of a computation using continuations, where `'S'`
-plays the role of a control operator with some similarities to what is
-often called `shift`.  In the analogy, the input list portrays a
+plays the role of a continuation operator. (It works like the Scheme operators `shift` or `control`; the differences between them don't manifest themselves in this example.) In the analogy, the input list portrays a
 sequence of functional applications, where `[f1; f2; f3; x]` represents
 `f1(f2(f3 x))`.  The limitation of the analogy is that it is only
 possible to represent computations in which the applications are
 always right-branching, i.e., the computation `((f1 f2) f3) x` cannot
 be directly represented.
 
-One possibile development is that we could add a special symbol `'#'`,
+One way to extend this exercise would be to add a special symbol `'#'`,
 and then the task would be to copy from the target `'S'` only back to
-the closest `'#'`.  This would allow the task to simulate delimited
-continuations with embedded prompts.
+the closest `'#'`.  This would allow our task to simulate delimited
+continuations with embedded `prompt`s (also called `reset`s).
 
 The reason the task is well-suited to the list zipper is in part
 because the list monad has an intimate connection with continuations.