X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=hints%2Fassignment_10_hint_3.mdwn;h=0523d67daf771c499b8d9ba34d4c49611df316cc;hp=6a442a096349f35d58bea8b8e4f386df98744eb3;hb=109034aa514a67fcaed0607b4deb4b339f67ab76;hpb=e8c44d33c131b04cf7b61f083a4a4f59d2484d84 diff --git a/hints/assignment_10_hint_3.mdwn b/hints/assignment_10_hint_3.mdwn index 6a442a09..0523d67d 100644 --- a/hints/assignment_10_hint_3.mdwn +++ b/hints/assignment_10_hint_3.mdwn @@ -43,22 +43,26 @@ While we're at it, let's re-define our Reader monad too: Now instead of annotating leaves with the current store, we'll convert the leaves into "askers" that will wait for an environment and return what that -environment says about the original leaf. At the same time, we'll update the store so that it knows how many leafs of each value have been seen: +environment says about the original leaf. At the same time, we'll update the store so that it knows how many leafs of each value have been seen. +We could proceed in two ways: we could either convert the leafs into "askers" now, and then use `TR.monadize` to conver the tree of askers into a tree-asker (that is, a function from an environment to a tree). Or we could just pass the leaves through unchanged for the moment, and leave the job of converting them to askers to the `TR.monadize` pass. We'll take the second strategy. (This turns out to fit better with what we go on to do later.) Hence, this first pass, using `TS.monadize`, only has to update the store. - let annotater : char -> ((char -> int) -> int) State_custom.m = - fun a s -> ((fun e -> e a), update_env s a);; + + let annotater : char -> char State_custom.m = + fun a s -> (a, update_env s a);; let v2 = TS.monadize annotater tree (fun a -> 0);; The seed function here is an environment that by default maps every leaf -element to 0. In the end, `v2` consists of a pair of a tree of askers, and a -`char -> int` environment. We can use `TR.monadize` to convert that tree of -askers into a tree-asker, that is, a function from an environment to a tree. And we then feed it the very environment that was `v2`'s final store: +element to 0. In the end, `v2` consists of a pair of a tree and a +`char -> int` environment. We can use `TR.monadize` to convert that tree into a tree-asker, that is, a function from an environment to a tree. And we then feed it the very environment that was `v2`'s final store: + + let asker : char -> int Reader_custom.m = + fun (a : char) -> fun (env : char -> int) -> env a;; - let (tree',env) = v2 - in TR.monadize (fun a -> a) tree' env;; + let (t, env) = v2 + in TR.monadize asker t env;; This gives us a tree of `int`s, where each `int` replaces the original `char`