[Haskell-beginners] merge two handles

Manfred Lotz manfred.lotz at arcor.de
Tue Jun 14 17:32:19 CEST 2011

On Mon, 13 Jun 2011 16:59:11 -0400
David McBride <dmcbride at neondsl.com> wrote:

> The IO Channel antoine recommended is a very elegant solution to this.
> main = do
>   let cmd = ..., parms =...
>   (_ ,Just hout ,Just herr ,p) <- createProcess (proc cmd parms)
>   chan <- newTChanIO :: IO (TChan String)
>   makeThread hout chan
>   makeThread herr chan
>   forever $ atomically $ readTchan chan >>= print
> makeThread handle chan = forkIO $ forever $ do
>   hGetLine handle >>= atomically (writeTchan chan)

Thanks for the code. I added

import GHC.Conc.Sync
import System.IO
import System.Environment
import System.Process
import Control.Monad
import Control.Concurrent.STM.TChan

but when compiling I get:

    Couldn't match expected type `STM a0' with actual type `IO ()'
    Expected type: String -> STM a0
      Actual type: String -> IO ()
    In the second argument of `(>>=)', namely `print'
    In the second argument of `($)', namely `readTChan chan >>= print'

    Couldn't match expected type `String -> IO a0'
                with actual type `IO a1'
    In the return type of a call of `atomically'
    In the second argument of `(>>=)', namely
      `atomically (writeTChan chan)'
    In the expression: hGetLine handle >>= atomically (writeTChan chan)

I'm still too much in the dark regarding the monad stuff thus having no
idea how to fix it.


More information about the Beginners mailing list