From ae36642e2abca2e552e48cfaca3265de603c35bb Mon Sep 17 00:00:00 2001 From: Jim Pryor Date: Wed, 1 Dec 2010 00:56:31 -0500 Subject: [PATCH] lists-to-contin tweaks Signed-off-by: Jim Pryor --- from_lists_to_continuations.mdwn | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/from_lists_to_continuations.mdwn b/from_lists_to_continuations.mdwn index 5bc49869..7873a2cd 100644 --- a/from_lists_to_continuations.mdwn +++ b/from_lists_to_continuations.mdwn @@ -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 @@ -186,10 +186,8 @@ 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)`. -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 -- 2.11.0