haskellmonadplus

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


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?


Solution

  • 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.