Search code examples
haskellinputiobuffering

How can I get input right when it is typed in Haskell?


I have written a Brainfuck interpreter in Haskell, but it only operates on the input once I hit Ctrl-D to signal EOF. How can I make the program act on each character when it is typed?

Here is the source. To use the program, give a file to interpret as an argument or type your program in the first line of stdin.


Solution

  • It sounds like your input is being buffered. You can modify the buffering mode of a file handle with System.IO.hSetBuffering. If you are reading from standard input, for instance, then you could disable buffering with:

    import System.IO
    
    hSetBuffering stdin NoBuffering