From: Jim Pryor Date: Sun, 21 Nov 2010 18:37:42 +0000 (-0500) Subject: week9 tweak X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=4d5d8744094b6273fdc7859930fd9376f6b627a1 week9 tweak Signed-off-by: Jim Pryor --- diff --git a/week9.mdwn b/week9.mdwn index a05c132c..2d3efc1e 100644 --- a/week9.mdwn +++ b/week9.mdwn @@ -414,7 +414,7 @@ This passes through the current store unaltered, and also returns a copy of the some_existing_state_monad_box >>= fun _ -> get_state >>= (fun cur_state -> ...) -The `fun _ ->` part here discards the value wrapped by `some_existing_state_monad_box`. We're only going to pass through, unaltered, whatever *store* is generated by that monadic value. We also wrap that store as *our own value*, which can be retrieved by further operations in the `... >>= ...` chain, such as `(fun cur_state -> ...)`. +The `fun _ ->` part here discards the value wrapped by `some_existing_state_monad_box`. We're only going to pass through, unaltered, whatever *store* is generated by that monadic box. We also wrap that store as *our own value*, which can be retrieved by further operations in the `... >>= ...` chain, such as `(fun cur_state -> ...)`. The other operation for the State monad will be to update the existing store to a new one. This operation looks like this: @@ -433,7 +433,7 @@ We can of course define more complex functions that perform the `get_state >>= ( In general, a State monadic **box** (type `'a state`, what appears at the start of a `... >>= ... >>= ...` chain) is an operation that accepts some starting store as input---where the store might be simple as it is here, or much more complex---and returns a value plus a possibly modified store. This can be thought of as an encoding of some operation on a store serving as a box wrapped around a value. -State monadic **operations** (type `'a -> 'b state`, what appears anywhere in the middle or end of a `... >>= ... >>= ...` chain) are operations that generate new State monadic values, based on what value was wrapped by the preceding elements in the `... >>= ... >>= ...` chain. The computations on a store that these encode (which their values may or may not be sensitive to) will be chained in the order given by their position in the `... >>= ... >>= ...` chain. That is, the computation encoded by the first element in the chain will accept a starting store s0 as input, and will return (a value and) a new store s1 as output, the next computation will get s1 as input and will return s2 as output, the next computation will get s2 as input, ... and so on. +State monadic **operations** (type `'a -> 'b state`, what appears anywhere in the middle or end of a `... >>= ... >>= ...` chain) are operations that generate new State monad boxes, based on what value was wrapped by the preceding elements in the `... >>= ... >>= ...` chain. The computations on a store that these encode (which their values may or may not be sensitive to) will be chained in the order given by their position in the `... >>= ... >>= ...` chain. That is, the computation encoded by the first element in the chain will accept a starting store s0 as input, and will return (a value and) a new store s1 as output, the next computation will get s1 as input and will return s2 as output, the next computation will get s2 as input, ... and so on. To get the whole process started, the complex computation so defined will need to be given a starting store. So we'd need to do something like this: