[Haskell] Control.Monad.Writer as Python generator

David Menendez zednenem at psualum.com
Thu Apr 14 01:05:46 EDT 2005


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 <zednenem at psualum.com> | "In this house, we obey the laws
<http://www.eyrie.org/~zednenem>      |        of thermodynamics!"


More information about the Haskell mailing list