3

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?

duplode
  • 33,731
  • 7
  • 79
  • 150
rampion
  • 87,131
  • 49
  • 199
  • 315
  • Is is just me or there seems to be a lot of haskell question saying "I haven't found it on Hoogle. So is it useful?". I did it myself. Answers are nonetheless interesting. – gawi Oct 24 '11 at 18:44

2 Answers2

1

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.

fuz
  • 88,405
  • 25
  • 200
  • 352
1

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.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431