X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=topics%2Fweek9_monad_transformers.mdwn;h=f9da05ad15f24c5c05b08a28e9da8051c96fd951;hp=b01904308de12bd76d439440ba58fffc7a618670;hb=fd2cb06c9e18732a6fbbf20da0b2f92dc981a5db;hpb=73dbb6a826f852b98672a36733be3d33b7cb00f6 diff --git a/topics/week9_monad_transformers.mdwn b/topics/week9_monad_transformers.mdwn index b0190430..f9da05ad 100644 --- a/topics/week9_monad_transformers.mdwn +++ b/topics/week9_monad_transformers.mdwn @@ -155,23 +155,23 @@ Here's an example wrapping Option around List, and vice versa: # module LO = Monad.List.T(Monad.Option);; # module OL = Monad.Option.P(Monad.List);; (* we use the `.P` transformer to hoist List's ++ operation *) - # OL.(run (mzero ++ mid 20 >>= fun i -> mid (i+10)));; + # OL.(run (mzero ++ mid 20 >>= fun x -> mid (x+10) ));; - : int OL.result = [Some 30] When List is on the inside, the failed results just got dropped and the computation proceeds without them. - # LO.(run (hoist Monad.Option.mzero ++ mid 20 >>= fun i -> mid (i+10)));; + # LO.(run (hoist Monad.Option.mzero ++ mid 20 >>= fun x -> mid (x+10) ));; - : int LO.result = None On the other hand, when Option is on the inside, as in LO, failures (which we again represent by `mzero`s from the Option monad, not the List monad's own `mzero`; but here since it's the inner monad we need to `hoist Monad.Option.mzero`) abort the whole computation. (If you instead used the List monad's `mzero`, it'd be ignored by `++` and you'd end with just `Some [30]`.) -This is fun. Notice the difference it makes whether the second `++` is native to the outer `Monad.List`, or whether it's the inner `Monad.List`'s `++` hoisted into the outer wrapper: +This is fun. Here is a List around a List. Notice the difference it makes whether the second `++` is native to the outer `Monad.List`, or whether it's the inner `Monad.List`'s `++` hoisted into the outer wrapper: # module LL = Monad.List.T(Monad.List);; # LL.(run(mid 1 ++ mid 2 >>= fun x -> mid x ++ mid (10*x) ));; - : int LL.result = \[[1; 10; 2; 20]] - # LL.(run(mid 1 ++ mid 2 >>= fun x -> hoist Monad.List.(mid x ++ mid (10*x) )));; + # LL.(run(mid 1 ++ mid 2 >>= fun x -> hoist Monad.List.(mid x ++ mid (10*x)) ));; - : int LL.result = [[1; 2]; [1; 20]; [10; 2]; [10; 20]]