Haskell: Is (MonadPlus m => Bool -> a -> m a) not useful?

277 views Asked by At

I was inspired to play with FizzBuzz after taking a gander at codepad.org, and found myself wanting some function:

mwhen :: MonadPlus m => Bool -> a -> m a
mwhen b = if b then return else const mzero

just so I could do mwhen (n /? 3) "Foo" `mappend` mwhen (n /? 5) "Bar"

I expected it to be up on hoogle, but no dice.

Is this not as useful as I'd think it'd be?

2

There are 2 answers

0
Daniel Fischer On BEST ANSWER

mwhen b a is exactly guard b >> return a. When you're doing more things after the guard, you normally would have bound the a before the mwhen and not need the return. So mwhen's usefulness seems to be mostly saving a few keystrokes at the end of do-blocks.

0
fuz On

A reason it does not exists is, that there is usually no need for this combinator. You can simply overwrite the result of a when with <$ or >>, and as you usually have many other monadic operations after a when.