state monad tutorial, records tweaks
authorJim Pryor <profjim@jimpryor.net>
Sun, 12 Dec 2010 16:45:53 +0000 (11:45 -0500)
committerJim Pryor <profjim@jimpryor.net>
Sun, 12 Dec 2010 16:45:53 +0000 (11:45 -0500)
Signed-off-by: Jim Pryor <profjim@jimpryor.net>
state_monad_tutorial.mdwn
translating_between_OCaml_Scheme_and_Haskell.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 ...
 
        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.
 
 
 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's try it out:
 
-       # let s0 = {total = 42; modifications = 3 };;
+       # let s0 = { total = 42; modifications = 3 };;
        # increment_store s0;;
        # 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:
 
 
 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:
 
 
 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.
        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.
index 69ba76d..338a296 100644 (file)
@@ -434,7 +434,7 @@ The syntax for declaring and using these is a little bit different in the two la
 
        In OCaml:
 
 
        In OCaml:
 
-               let { red = r; green = g } = c
+               let { red = r; green = g; _ } = c
                in r
 
        In Haskell:
                in r
 
        In Haskell:
@@ -448,7 +448,7 @@ The syntax for declaring and using these is a little bit different in the two la
 
        In OCaml it's:
 
 
        In OCaml it's:
 
-               # let makegray ({red = r} as c) = { c with green=r; blue=r };;
+               # let makegray ({ red = r; _ } as c) = { c with green=r; blue=r };;
                val makegray : color -> color = <fun>
                # makegray { red = 0; green = 127; blue = 255 };;
                - : color = {red = 0; green = 0; blue = 0}
                val makegray : color -> color = <fun>
                # makegray { red = 0; green = 127; blue = 255 };;
                - : color = {red = 0; green = 0; blue = 0}
@@ -466,9 +466,9 @@ The syntax for declaring and using these is a little bit different in the two la
        
        and then extract the field you want using pattern-matching:
 
        
        and then extract the field you want using pattern-matching:
 
-               let Color(r,_,_) = c;;
+               let Color (r, _, _) = c;;
                (* or *)
                (* or *)
-               match c with Color(r,_,_) -> ...
+               match c with Color (r, _, _) -> ...
 
        (Or you could just use bare tuples, without the `Color` data constructor.)
 
 
        (Or you could just use bare tuples, without the `Color` data constructor.)