I would like my commandline Haskell program to function like this:
program wait for user input,
- user type something, push “enter”
- Haskell process the input, shows the result on stdout
- Haskell waits for next user input
- If no more input, user terminate program by Ctrl+D
I tried getContents. But getContents wait for user to type all lines before processing them.
There’s a lot of confusion going on here. Let’s try to clear things up.
The most likely thing here is that you’ve compiled your program, and didn’t notice that the default buffering for output was block-buffering. This is easy to fix:
You should use
NoBufferingif you don’t plan on printing a whole line (including newline) after each line of user input.For a more precise answer, we’ll need to see the code you tried that didn’t work.
This answer is not quite correct. The real answer is that
showproduces a string with no newlines in it! (Though the character sequence['\\','\n']does show up sometimes if the input is more than one line long.) So, forinteract show, you really must useNoBufferingonstdout. For example, if you use this:…the program will print a bit more output after each line. You might also want to set
stdin‘s buffering toNoBuffering(instead of the defaultLineBuffering), sinceshowis productive enough that it really can produce more output after each keystroke.