[Haskell-beginners] Haskeline and forkIO
Neeraj Rao
neeraj2608 at gmail.com
Wed Sep 3 16:51:50 UTC 2014
As a Haskell beginner, I would also like to recommend HLint as a very
useful tool to catch these kinds of things.
On Sep 3, 2014 12:25 PM, "Peter Jones" <mlists at pmade.com> wrote:
> "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
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140903/7a807ade/attachment.html>
More information about the Beginners
mailing list