[Haskell] ANN: monadLib 2.0

Udo Stenzel u.stenzel at web.de
Thu Mar 23 14:08:51 EST 2006


Iavor Diatchki wrote:
> >
> > *> when :: Monad m => m a -> Bool -> m (Maybe a)
> >
> 
> I am not sure if this would be useful.  If you cared about the result
> you would have to write:
> do { x <- when p m; case x of { Just a -> e1; Nothing -> e2. }

...unless you wanted the result in the form of a Maybe.  Since there
really is no other sensible choice for the "False" branch, I contend
that _if_ you want the result at all, you want is as a maybe.  If not,
well, then ignoring (Maybe a) isn't any harder than ignoring (); (>>)
already accomplishes that.


> A simpler and (probably) more efficient way to write the above is:
> do { if p then do { a <- m; e1} else e2 }

But usually p comes from a monadic computation, so in reality you need

*> do { p' <- p ; if p' then do { a <- m ; e1 } else e2 }

which I find excessively ugly.  Actually this is not a case for when,
it's one for cond:

*> cond t f True = t
*> cond t f False = f

*> p >>= cond (do { a <- m ; e1 }) e2


> The main point of this discussion has been:
>  (i) Should the ignoring be done silently in the library, or
>  (ii) should the programmer be explicit (i.e., write 'm >> return ()').

I just think that both options are wrong.

(iii) should the programmer be explicit (ie. write 'when p m >>')


Udo.
-- 
Das Elend hat viele Gesichter, wie gefällt Ihnen meins?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/libraries/attachments/20060323/97b2cdff/attachment.bin


More information about the Libraries mailing list