[Haskell-cafe] Re: 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 18:50:08 EDT 2009
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 <tphyahoo at gmail.com>:
> 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