
correction, should be:
iterateNTimes i f x = foldr (.) id (replicate i f) $ x
tntIO :: IO Int
-- same as replicateM (10^6) $ return 0
tntIO = return . head =<< (iterateNTimes (10^6) (ap . liftM (:) .
return $ 0) (return [])) -- produces output
tntMb :: Maybe Int -- overflows
tntMb = return . head =<< (iterateNTimes (10^6) (ap . liftM (:) .
return $ 0) (return [])) -- stack overflow
which now compiles.
2009/10/13 Thomas Hartman
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