Manufacturer and consumer issues in Haskell?

I would like how we can implement producer / consumer in a functional programming language such as Haskell? and how will it differ from the imperative language? My understanding of functional programming language is primitive. Any help would be appreciated.

+7
multithreading design concurrency functional-programming haskell
source share
3 answers

Manufacturer / consumer abstraction using preventive streams and messages transmitted over the channel:

import Data.Char import Control.Concurrent import Control.Concurrent.Chan main = do c <- newChan cs <- getChanContents c -- a lazy stream of events from eventReader forkIO (producer c) -- char producer consumer cs where -- thread one: the event producer producer c = forever $ do key <- getChar writeChan c key -- thread two: the lazy consumer consumer = mapM_ print . map shift where shift c | isAlpha c = chr (ord c + 1) | otherwise = c 

You would use a similar model in Erlang. Topics for representing the consumer and the manufacturer and a common channel of messages between them, each of which acts asynchronously.

+14
source share

I will add to dons excellent answer that the main mechanism here is what is called MVar , and it is a required parallel container for the value. You "put" and "receive" in MVar and exit it. Getting empty MVar blocks, like a complete set. It is both a communication mechanism and a synchronization mechanism. I believe that it was invented by Arvind as part of the Monsoon / * t project. There is a beautiful book by Nikhil and Arvind that explains their dialect of the pH level of a parallel Haskell. Many ideas have been accepted at the GHC, and the book is worth reading.

+6
source share

In addition to the stress on the state mentioned by Norman and Don, you can also come up with the normal functioning of the application and laziness as a producer and consumer.

Here is the manufacturer of natural numbers:

 nats = [1..] 

And here is a consumer who calculates the squares of these numbers:

 squares = map (\x -> x * x) nats 

Manufacturers, such as yield return in C # or generators in Python, can often be expressed as follows: like simple lazy lists in Haskell.

+2
source share

All Articles