From 4d995119271f7c35e21a6775ee2454b4774b13a6 Mon Sep 17 00:00:00 2001 From: Jim Pryor Date: Wed, 1 Dec 2010 00:33:01 -0500 Subject: [PATCH] lists-to-contin tweaks Signed-off-by: Jim Pryor --- from_lists_to_continuations.mdwn | 41 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/from_lists_to_continuations.mdwn b/from_lists_to_continuations.mdwn index be7e17f5..8c84dfbd 100644 --- a/from_lists_to_continuations.mdwn +++ b/from_lists_to_continuations.mdwn @@ -75,9 +75,10 @@ entire list has been unzipped (and so the zipped half of the zipper is empty). type 'a list_zipper = ('a list) * ('a list);; let rec tz (z:char list_zipper) = - match z with (unzipped, []) -> List.rev(unzipped) (* Done! *) - | (unzipped, 'S'::zipped) -> tz ((List.append unzipped unzipped), zipped) - | (unzipped, target::zipped) -> tz (target::unzipped, zipped);; (* Pull zipper *) + match z with + | (unzipped, []) -> List.rev(unzipped) (* Done! *) + | (unzipped, 'S'::zipped) -> tz ((List.append unzipped unzipped), zipped) + | (unzipped, target::zipped) -> tz (target::unzipped, zipped);; (* Pull zipper *) # tz ([], ['a'; 'b'; 'S'; 'd']);; - : char list = ['a'; 'b'; 'a'; 'b'; 'd'] @@ -152,23 +153,23 @@ The structure and the behavior will follow that of `tz` above, with some small but interesting differences. We've included the orginal `tz` to facilitate detailed comparison: -
-let rec tz (z:char list_zipper) = 
-    match z with (unzipped, []) -> List.rev(unzipped) (* Done! *)
-               | (unzipped, 'S'::zipped) -> tz ((List.append unzipped unzipped), zipped) 
-               | (unzipped, target::zipped) -> tz (target::unzipped, zipped);; (* Pull zipper *)
-
-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));;
-
-# tc ['a'; 'b'; 'S'; 'd'] (fun x -> x);;
-- : char list = ['a'; 'b'; 'a'; 'b']
-
-# tc ['a'; 'S'; 'b'; 'S'] (fun x -> x);;
-- : char list = ['a'; 'a'; 'b'; 'a'; 'a'; 'b']
-
+ let rec tz (z:char list_zipper) = + match z with + | (unzipped, []) -> List.rev(unzipped) (* Done! *) + | (unzipped, 'S'::zipped) -> tz ((List.append unzipped unzipped), zipped) + | (unzipped, target::zipped) -> tz (target::unzipped, zipped);; (* Pull zipper *) + + 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));; + + # tc ['a'; 'b'; 'S'; 'd'] (fun x -> x);; + - : char list = ['a'; 'b'; 'a'; 'b'] + + # tc ['a'; 'S'; 'b'; 'S'] (fun x -> x);; + - : char list = ['a'; 'a'; 'b'; 'a'; 'a'; 'b'] To emphasize the parallel, I've re-used the names `zipped` and `target`. The trace of the procedure will show that these variables -- 2.11.0