X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=manipulating_trees_with_monads.mdwn;h=35dff5d9fa3fb1c02a7fba5a64008b949b90e1a5;hp=23abaa63ed33444ca1b3a6dd69fad974f4a8bb9d;hb=434fc9bef584f51ac5338a39dc4ff5da44b5b435;hpb=2f6e770665caa164a2c608417954b24359b10f56 diff --git a/manipulating_trees_with_monads.mdwn b/manipulating_trees_with_monads.mdwn index 23abaa63..35dff5d9 100644 --- a/manipulating_trees_with_monads.mdwn +++ b/manipulating_trees_with_monads.mdwn @@ -133,8 +133,8 @@ of that function. It would be a simple matter to turn an *integer* into an `int reader`: - let int_readerize : int -> int reader = fun (a : int) -> fun (modifier : int -> int) -> modifier a;; - int_readerize 2 (fun i -> i + i);; + let get_int : int -> int reader = fun (a : int) -> fun (modifier : int -> int) -> modifier a;; + get_int 2 (fun i -> i + i);; - : int = 4 But how do we do the analagous transformation when our `int`s are scattered over the leaves of a tree? How do we turn an `int tree` into a reader? @@ -185,17 +185,17 @@ Then we can expect that supplying it to our `int tree reader` will double all th In more fanciful terms, the `tree_monadize` function builds plumbing that connects all of the leaves of a tree into one connected monadic network; it threads the `'b reader` monad through the original tree's leaves. - # tree_monadize t1 int_readerize double;; + # tree_monadize t1 get_int double;; - : int tree = Node (Node (Leaf 4, Leaf 6), Node (Leaf 10, Node (Leaf 14, Leaf 22))) Here, our environment is the doubling function (`fun i -> i + i`). If we apply the very same `int tree reader` (namely, `tree_monadize -t1 int_readerize`) to a different `int -> int` function---say, the +t1 get_int`) to a different `int -> int` function---say, the squaring function, `fun i -> i * i`---we get an entirely different result: - # tree_monadize t1 int_readerize square;; + # tree_monadize t1 get_int square;; - : int tree = Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))