Using foldl to add numbers, while validating that they are nonnegative
import Data.Foldable

addAll :: [Int] -&gt; Either String Int                                                                                                                          
addAll = foldlM safeAdd 0 where
  safeAdd :: Int -&gt; Int -&gt; Either String Int
  safeAdd n next = if next &lt; 0
    then Left (show next ++ " is not positive")
    else Right (n + next)


Refer http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Foldable.html#v:foldlM