title: Folding across Maybes in Haskell
body: <p>In an attempt to learn Haskell, I have come across a situation in which I wish to do a fold over a list but my accumulator is a Maybe. The function I'm folding with however takes in the "extracted" value in the Maybe and if one fails they all fail. I have a solution I find kludgy, but knowing as little Haskell as I do, I believe there should be a better way. Say we have the following toy problem: we want to sum a list, but fours for some reason are bad, so if we attempt to sum in a four at any time we want to return Nothing. My current solution is as follows:</p>
  
  <pre><code>import Maybe
  
  explodingFourSum :: [Int] -&gt; Maybe Int
  explodingFourSum numberList =
      foldl explodingFourMonAdd (Just 0) numberList
      where explodingFourMonAdd =
          (\x y -&gt; if isNothing x
                      then Nothing
                      else explodingFourAdd (fromJust x) y)
  
  explodingFourAdd :: Int -&gt; Int -&gt; Maybe Int
  explodingFourAdd _ 4 = Nothing
  explodingFourAdd x y = Just(x + y)
  </code></pre>
  
  <p>So basically, is there a way to clean up, or eliminate, the lambda in the <code>explodingFourMonAdd</code> using some kind of Monad fold? Or somehow currying in the >>=
  operator so that the fold behaves like a list of functions chained by >>=?</p>