title: IO and Maybe monad interaction
body: <p>I have the following code but I feel it is too ugly and imperative.  Would anybody rephrase it to be more functional?   (I messed with MaybeT but could not make it work)  Applicative answers welcome as well.</p>
  
  <pre><code>getString :: IO String
  
  pred :: String -&gt; Bool
  
  f :: String -&gt; String
  
  result :: IO (Maybe String)
  result = do
    s &lt;- getString
    if pred s
      then return $ Just $ f s
      else return Nothing
  </code></pre>
  
  <p>EDIT: A follow-up question: what if both pred and f also return results within IO (should I split this out into a separate question?)</p>
  
  <pre><code>getString :: IO String
  
  pred :: String -&gt; IO Bool
  
  f :: String -&gt; IO String
  
  result :: IO (Maybe String)
  result = do
    s &lt;- getString
    b &lt;- pred s
    if b
      then Just &lt;$&gt; f s
      else return Nothing
  </code></pre>