[Haskell-beginners] Haskeline and forkIO

Jeff C. Britton jcb at iteris.com
Wed Aug 27 19:03:49 UTC 2014


If I modify the code to the suggested form:

    let f = runWorker (worker path)
    in
        do
          f
          loop

then I get the following compile error

Couldn't match type `IO' with `InputT IO'
Expected type: InputT IO ()
  Actual type: IO ()
In a stmt of a 'do' block: f
In the expression:
  do { f;
       loop }

I have also tried
    let f = runWorker (worker path)
    in
        do
          return f
          loop

This compiles, but won't run forkIO.

I can't figure out how to get forkIO to run and make the type system happy.


I am going to past the code again, because the line breaks got messed up the first time.
I have removed the `seq` call though as it simplifies the code a bit.

import Control.Concurrent (forkIO)
import Control.Exception
import qualified Data.ByteString.Lazy as L
import System.Console.Haskeline hiding (handle)

-- Provided by the 'zlib' package on http://hackage.haskell.org/
import Codec.Compression.GZip (compress)

worker :: FilePath -> IO ()
worker path = L.readFile path >>= L.writeFile (path ++ ".gz") . compress

runWorker :: FilePath -> IO()
runWorker path = handle (print :: SomeException -> IO ()) $ do
    forkIO (worker path)
    return ()

loop :: InputT IO ()
loop = do
    maybeLine <- getInputLine "Enter a file to compress> "
    case maybeLine of
      Nothing -> return ()      -- user entered EOF
      Just "" -> return ()      -- treat no name as "want to quit" 
      Just path -> do
            return (runWorker path)
            loop

main = runInputT defaultSettings loop




More information about the Beginners mailing list