[Haskell] ANN: monadLib 2.0

Iavor Diatchki iavor.diatchki at gmail.com
Thu Mar 23 13:48:33 EST 2006


Hello,

On 3/22/06, Udo Stenzel <u.stenzel at web.de> wrote:
> What about this?
>
> *> when :: Monad m => m a -> Bool -> m (Maybe a)
>
> It doesn't ignore any meaningful result, ignoring the (Maybe a) would be
> explicit through the use of (>>).

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. }
This is not good, because you are performing two tests (is 'p' true,
did we get a 'Just'),
when there should be only one.  Furthermore this seems difficult to
optmize away.

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

I think that 'when' (and 'whenM') are usually used when the
computation does not return a result, so it is reasobale that the
final result is of type 'm ()".  In some (rare) situation we really
mean to ignore the result of the body of the 'when'. 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 don't think either way matters much, but I tend to lean towards the
status quo, i.e. programmers have to be explicit.

-Iavor


More information about the Libraries mailing list