state monad tutorial, records tweaks
[lambda.git] / state_monad_tutorial.mdwn
index c89ce6c..bac9124 100644 (file)
@@ -42,7 +42,7 @@ Of course you can do this:
        in let final_result = Monad.run v 0
        in ...
 
-The other heavyweight way to encapsulate the type of a monad is to use records. See [here] (/translating_between_OCaml_Scheme_and_Haskell) and [here](/coroutines_and_aborts/) for some introduction to these. We don't use this design in our OCaml monad library, but the Haskell monad libraries do, and it would be good for you to get acquainted with it so that you can see how to ignore it when you come across it in Haskell-based literature. (Or you might want to learn Haskell, who knows?)
+The other heavyweight way to encapsulate the type of a monad is to use records. See [here](/translating_between_OCaml_Scheme_and_Haskell) and [here](/coroutines_and_aborts/) for some introduction to these. We don't use this design in our OCaml monad library, but the Haskell monad libraries do, and it would be good for you to get acquainted with it so that you can see how to ignore it when you come across it in Haskell-based literature. (Or you might want to learn Haskell, who knows?)
 
 We'll illustrate this technique in OCaml code, for uniformity. See the [translation page](/translating_between_OCaml_Scheme_and_Haskell) about how this looks in Haskell.
 
@@ -105,9 +105,9 @@ Here is how you'd have to do it using our OCaml monad library:
 
 Let's try it out:
 
-       # let s0 = {total = 42; modifications = 3 };;
+       # let s0 = { total = 42; modifications = 3 };;
        # increment_store s0;;
-       - : int * store' = (42, {total = 43; modifications = 4})
+       - : int * store' = (42, {total = 43; modifications = 4})
 
 Or if you used the OCaml monad library:
 
@@ -126,7 +126,12 @@ That ensures that the value we get at the end is the value returned by the first
 
 You should start to see here how chaining monadic values together gives us a kind of programming language. Of course, it's a cumbersome programming language. It'd be much easier to write, directly in OCaml:
 
-       let { total = value } = s0
+       let value = s0.total
+       in (value, { total = s0.total + 2; modifications = s0.modifications + 2};;
+
+or, using pattern-matching on the record (you don't have to specify every field in the record):
+
+       let { total = value; _ } = s0
        in (value, { total = s0.total + 2; modifications = s0.modifications + 2};;
 
 But **the point of learning how to do this monadically** is that (1) monads show us how to embed more sophisticated programming techniques, such as imperative state and continuations, into frameworks that don't natively possess them (such as the set-theoretic metalanguage of Groenendijk, Stockhof and Veltman's paper); and (2) monads are delicious.