
23 Mar
2006
23 Mar
'06
2:51 p.m.
Hello,
On 3/23/06, Udo Stenzel
Iavor Diatchki wrote: ... 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
In the Monad.Combinatros module of 'monadLib' there is the 'ifM' combinator which is kind of like the above: ifM :: Monad m => m Bool -> m a -> m a -> m a You could write: ifM p (do { a <- m; e1 }) e2 The combinator 'whenM' is just a special case: whenM p m = ifM p m (return ()) . -Iavor