Signed-off-by: Jim Pryor <profjim@jimpryor.net>
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.
- # let s0 = {total = 42; modifications = 3 };;
+ # let s0 = { total = 42; modifications = 3 };;
- # - : 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:
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.
- let { red = r; green = g } = c
+ let { red = r; green = g; _ } = c
- # 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}
and then extract the field you want using pattern-matching:
and then extract the field you want using pattern-matching:
+ let Color (r, _, _) = c;;
- 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.)