From: Jim Pryor Date: Sun, 12 Dec 2010 16:45:53 +0000 (-0500) Subject: state monad tutorial, records tweaks X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=e4a25b6b413f5e536393226994c171685e50c797;hp=d0c1b524d79cd88b2979d7825dc297bbbddc4f22 state monad tutorial, records tweaks Signed-off-by: Jim Pryor --- diff --git a/state_monad_tutorial.mdwn b/state_monad_tutorial.mdwn index c89ce6c4..bac9124e 100644 --- a/state_monad_tutorial.mdwn +++ b/state_monad_tutorial.mdwn @@ -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. diff --git a/translating_between_OCaml_Scheme_and_Haskell.mdwn b/translating_between_OCaml_Scheme_and_Haskell.mdwn index 69ba76d4..338a2966 100644 --- a/translating_between_OCaml_Scheme_and_Haskell.mdwn +++ b/translating_between_OCaml_Scheme_and_Haskell.mdwn @@ -434,7 +434,7 @@ The syntax for declaring and using these is a little bit different in the two la In OCaml: - let { red = r; green = g } = c + let { red = r; green = g; _ } = c 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: - # 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 = # 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: - let Color(r,_,_) = c;; + let Color (r, _, _) = c;; (* or *) - match c with Color(r,_,_) -> ... + match c with Color (r, _, _) -> ... (Or you could just use bare tuples, without the `Color` data constructor.)