title: My safeMapM won't catch any exceptions
body: <p>I'm trying to make a safe version of <code>mapM</code> that will exclude an element from the result if it throws an exception when executed.</p>
  
  <pre><code>safeMapM :: (a -&gt; IO b) -&gt; [a] -&gt; IO [b]
  safeMapM f []       = return []
  safeMapM f (x : xs) = do
      restResult &lt;- safeMapM f xs
      appliedResult &lt;- onException (f x &gt;&gt;= evaluate . Just) (return Nothing)
      case appliedResult of
          Just x' -&gt; return $ x' : restResult
          Nothing -&gt; return restResult
  </code></pre>
  
  <p>It fails to catch anything though. In a simple test case:</p>
  
  <pre><code>safeMapM (\n -&gt; return $ if n == 3 then error $ show n else n) [1,2,3,4,5]
  </code></pre>
  
  <p>it fails with:</p>
  
  <pre><code>*** Exception: 3
  [1,2,"ghci&gt;" 
  </code></pre>
  
  <p>Why isn't it being caught? Does <code>evaluate</code> not evaluate it fully enough for the <code>error</code> to be caught? Is there a way around this without requiring that the signature be changed to <code>safeMapM :: (NFData a, NFData b) =&gt; (a -&gt; IO b) -&gt; [a] -&gt; IO [b]</code>, and using <code>deepseq</code>?</p>