Consider following example:
safeMapM f xs = safeMapM' xs []
where safeMapM' [] acc = return $ reverse acc
safeMapM' (x:xs) acc = do y <- f x
safeMapM' xs (y:acc)
mapM return largelist -- Causes stack space overflow on large lists
safeMapM return largelist -- Seems to work fine
Using mapM on large lists causes a stack space overflow while safeMapM seems to work fine (using GHC 7.6.1 with -O2). However I was not able to find a function similar to safeMapM in the Haskell standard libraries.
Is it still considered good practice to use mapM (or sequence for that matter)?
If so, why is it considered to be good practice despite the danger of stack space overflows?
If not which alternative do you suggest to use?
mapMis faster if it doesn't overflow because you don't have toreverse? Did you measure it? - Niklas B.Mainmodule you used to test? - jberrymanControl.Monad.State.Lazy) where something liketake 100 <$> mapM id [1..]terminates.take 100 <$> safeMapM id [1..]cannot possibly terminate, regardless of the monad - Niklas B.main = mapM return [1..10000000] >> return ()- jonnydee