[Haskell-beginners] Haskeline and forkIO
Peter Jones
mlists at pmade.com
Wed Sep 3 16:24:55 UTC 2014
"Jeff C. Britton" <jcb at iteris.com> writes:
> That suggestion works.
> I will have to continue learning more about Monads.
I should have also mentioned that if you enable GHC warnings it should
tell you that having the `return` before `loop` is discarding the value
given to it.
> -----Original Message-----
> From: Beginners [mailto:beginners-bounces at haskell.org]
> 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
More information about the Beginners
mailing list