[Haskell-cafe] why does "iterateNTimes (10^6) (ap . liftM (:) . return $ 0) (return [])" produce output in the IO monad, but stack overflow in the maybe monad?

Thomas Hartman tphyahoo at gmail.com
Tue Oct 13 17:57:21 EDT 2009


Can someone explain why the one stack overflows and the other one doesn't?

iterateNTimes i f x = foldr (.) id (replicate i f) $ x
tntIO :: IO Int
-- same as replicateM (10^6) $ return 0 , and same as sequence .
replicate (10^6) $ return 0
tntIO = iterateNTimes (10^6) (ap . liftM (:) . return $ ) (return [])
-- produces output
tntMb :: Maybe Int -- overflows
tntMb = iterateNTimes (10^6) (ap . liftM (:) . return $ ) (return [])
-- stack overflow


More information about the Haskell-Cafe mailing list