views:

295

answers:

2

I've recently started to learn Haskell. I have this code

module Main
    where

import IO

main =  do 
            hSetBuffering stdin LineBuffering
            putStrLn "Please enter your name: "
            name <- getLine
            putStrLn ("Hello, " ++ name ++ ", how are you?")

I'm using the GHC compiler together with the notepad++ editor. The problem is the interaction goes like this:

Process started >>>
Vlad
Please enter your name:
Hello, Vlad, how are you?
<<< Process finished.

As you can see, output is only written after I input something. This was a bit unexpected, as I was sure the program would first ask for my name, then I'd get to enter it and then it would say hello. Well, that's exactly what happens if I run the exe manually, yet not if I run it with notepad++ and use its console wrapper...

How can I make notepad++ display the output when it should, and not all of it just before the program terminates? Is this even possible?

+5  A: 

Try setting stdout to LineBuffering! Also, loading your program in ghci instead runnign the compiled version doesn't seem to need any buffering at all...

By the way, I didn't know about the console in NPP - thanks for pointing me to it!

yatima2975
That worked, thank you. That's true, it doesn't, I don't know why it matters in notepad++. I added that because this http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf (at page 40 something) says it should fix exactly this problem, but I didn't think of also LineBuffering stdout.
IVlad
Well actually, that doesn't say it should fix exactly this. It says it should fix errors where reading input is not finished after entering a single line. So I guess it makes sense I should be LineBuffering stdout in this case. Thanks again!
IVlad
+1  A: 

I'm not familiar with notepad++, but a quick and hacky method would probably be to do

hFlush stdout

after each putStrLn. You could even make the following method:

nppPutStrLn s = putStrLn s >> hFlush stdout
sfultong