[Haskell-beginners] Strange difference in behaviour between ghc and ghci

Henry Lockyer henry.lockyer at ntlworld.com
Thu Jun 7 13:40:16 CEST 2012


in other words, in your version "What's your name?" *has* been put in the IO output buffer but this has not been flushed
to the terminal yet. You then input your name (without having seen the prompt you were expecting (yet)) and then
the newline of your subsequent putStrLn flushes both bits of output at the same time.

On 7 Jun 2012, at 12:32, Henry Lockyer wrote:

> A very quick reply and without reading carefully considering what may be different in GHCI  (!), 
> but the output buffer flushes (at least on my system) with newline  (if you don;t flush it 'manually').
> I suspect if you use putStrLn for 'what's your name' you may get something more like what you want.
> 
> 
> On 7 Jun 2012, at 12:22, Matthew Moppett wrote:
> 
>> I have a very simple Haskell file (HelloWorld.hs) that reads like this:
>> 
>> main = do
>>    putStr "What's your name? "
>>    n <- getLine
>>    putStrLn $ "Pleased to meet you, " ++ n
>> 
>> When I load it into ghci, I get the following result, as expected:
>> 
>> [1 of 1] Compiling Main             ( HelloWorld.hs, interpreted )
>> Ok, modules loaded: Main.
>> *Main> main
>> What's your name? Matt
>> Pleased to meet you, Matt
>> *Main> 
>> 
>> However, when I compile the same file using ghc and run it in a terminal, I get a very different result:
>> 
>> matt at matt-Lenovo-G575:~/Haskell$ ghc HelloWorld.hs
>> [1 of 1] Compiling Main             ( HelloWorld.hs, HelloWorld.o )
>> Linking HelloWorld ...
>> matt at matt-Lenovo-G575:~/Haskell$ ./HelloWorld
>> Matt
>> What's your name? Pleased to meet you, Matt
>> matt at matt-Lenovo-G575:~/Haskell$ 
>> 
>> 
>> -- in other words, the getLine action is being run before the putStr action, for some strange reason.
>> 
>> Is this a bug? Can anyone enlighten me as to what might be going on?
>> 
>> Regards,
>> 
>> Matt.
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
> 
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list