[Haskell-beginners] Haskeline and forkIO

Jeff C. Britton jcb at iteris.com
Tue Sep 2 18:19:23 UTC 2014


Thanks Peter,

That suggestion works.
I will have to continue learning more about Monads.

--Jeff


-----Original Message-----
From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of Peter Jones
Sent: Thursday, August 28, 2014 7:11 AM
To: beginners at haskell.org
Subject: Re: [Haskell-beginners] Haskeline and forkIO

"Jeff C. Britton" <jcb at iteris.com> writes:
> 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

The other issue you're having is because `runWorker path` is an `IO ()` value but at the point where you use it in the code the type system wants an `InputT IO ()`.  To try to satisfy the type system you used `return` to build a `InputT IO (IO ())` value, but that doesn't actually work (as you've noticed).  Since `InputT` is a transformer you have an extra layer to work through and so need to *lift* your `IO ()` value into the `InputT IO` layer.  Try this:

    -- Add this import
    import Control.Monad.IO.Class
    
    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
                liftIO (runWorker path)
                loop
    

You can think of `liftIO` as having this signature (in this context):

    liftIO :: IO () -> InputT IO ()
    
--
Peter Jones, Founder, Devalot.com
Defending the honor of good code

_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://www.haskell.org/mailman/listinfo/beginners


More information about the Beginners mailing list