4

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?

IVlad
  • 43,099
  • 13
  • 111
  • 179

2 Answers2

7

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
  • 6,580
  • 21
  • 42
  • 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 May 10 '10 at 13:23
  • 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 May 10 '10 at 13:32
2

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
  • 142
  • 1
  • 4