Thanks for the Cont example, David.  But...

The MonadCont is clever and it works ... but then fails -- ghci does not garbage collect and it blows up.
With the MonadCont version I can count up to 10^7 zeros:


*Main> length $ take (10^7) zerosInf
10000000
(26.20 secs, 0 bytes)

But this increases RSIZE of ghc-6.4 to 165MB. The 10^8 version goes to swap space and I had to kill it. My original MonadWriter version does not increase RSIZE when run (constant space), so the garbage collection must be working, and it is O(N) in the # of zeros counted:

*Main> length $ take (10^7) zerosInf
10000000
(1.22 secs, 0 bytes)
*Main> length $ take (10^8) zerosInf
100000000
(10.05 secs, 0 bytes)
*Main> length $ take (10^9) zerosInf
1000000000
(109.83 secs, 6 bytes)

--
Chris


On Apr 14, 2005, at 1:05 AM, David Menendez wrote:

ChrisK writes:

I was thinking to myself:
What in Haskell would give me a "yield" command like a Python
generator?

And the answer was "tell" in Control.Monad.Writer -- and I wrote some
simple examples (see below).

Another possibility would be a continuation monad.

    import Control.Monad.Cont

    yield :: a -> Cont [a] ()
    yield x = Cont (\c -> x : c ())

asGenerator :: Cont [a] v -> [a]
asGenerator (Cont f) = f (const [])
--
David Menendez <[EMAIL PROTECTED]> | "In this house, we obey the laws
<http://www.eyrie.org/~zednenem> | of thermodynamics!"

_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell

Reply via email to