[Haskell] Simple question about IO in hugs and ghc
Glynn Clements
glynn.clements at virgin.net
Sun Sep 26 18:52:29 EDT 2004
Vivian McPhail wrote:
> When I run my main function I get an error:
>
> The prompt does not wait for user input and thus my parser fails with an
> empty head.
>
> Is there a switch to not echo to the console? Is this the problem? It
> happens in both Hugs and GHC
>
> main :: IO ()
> main = do
> putStr "sentence? "
> input <- getLine
> case input of
> "quit" -> putStr "bye.\n"
> _ -> do
> let res = get_result $ proof $ do_proof $ getInput input
> in if res == Nothing
> then putStr ("-> Nothing\n")
> else putStr ("-> " ++ (show $ unJust res) ++ "\n")
> main
The problem isn't in the code above, so it must lie in one of the
functions which you don't show. For comparison, the following code
works (from within Hugs and ghci):
> main :: IO ()
> main = do
> putStr "sentence? "
> input <- getLine
> case input of
> "quit" -> putStr "bye.\n"
> _ -> do
> putStrLn input
> main
However, for it to work when compiled with ghc, you need to add
"hFlush stdout" after printing the prompt (hugs and ghci make stdout
unbuffered, but it will be line-buffered for a compiled program).
--
Glynn Clements <glynn.clements at virgin.net>
More information about the Haskell
mailing list