[Haskell-beginners] Parallel and Concurrent Programming in Haskell - Rate-Limiting the Producer

Kilian Gebhardt kilian.gebhardt at mailbox.tu-dresden.de
Mon Jan 27 14:31:29 UTC 2014


Hi,
I'm working through Simon Marlows Parallel and Concurrent Programming in
Haskell.
In Chapter 4, p. 69, or
http://chimera.labs.oreilly.com/books/1230000000929/ch04.html#_rate_limiting_the_producer
there is the task to extend a stream processing module based on
Control.Monad.Par in such a way that the producer is rate-limited, i.e.
the producer should not produce more than the consumer can consume.

The book suggests the following type:
data IList a
    = Nil
    | Cons a (IVar (IList a))
    | Fork (Par ()) (IList a)

I struggle to construct the functions for this type, as I don't know
what to do with the IList in the Fork case. It seems more natural to me,
to have an IVar (IList a) instead, whose value is provided by the
computation in Par () in the following way:

Fork (Par ()) (IVar (IList a))

streamFromListLimited :: NFData a => Int -> Int -> [a] -> Par (Stream a)
streamFromListLimited f d xs = do
    var <- new
    fork $ loop f xs var
    return var
  where
    loop :: NFData a => Int -> [a] -> Stream a -> Par ()
    loop _ []     var = put var Nil
    loop 0 (x:xs) var = do
        tail <- new
        put var (Fork (loop d xs tail) tail)
    loop n (x:xs) var = do
        tail <- new
        put var (Cons x tail)
        loop (n-1) xs tail

Can someone give me a hint?
Thanks,
Kilian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140127/eacdb79a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140127/eacdb79a/attachment.sig>


More information about the Beginners mailing list