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

Tim Perry tim.v2.0 at gmail.com
Wed Jan 29 18:38:25 UTC 2014


I suspect you need to post this somewhere besides Haskell-Beginners. Good
luck.

Tim

On Mon, Jan 27, 2014 at 6:31 AM, Kilian Gebhardt <
kilian.gebhardt at mailbox.tu-dresden.de> wrote:

>  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_producerthere 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
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140129/f8ae18ae/attachment.html>


More information about the Beginners mailing list