1

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.

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that, but it still waited until I hit Ctrl-D before giving a response to my input.
I changed stdout's buffering mode to NoBuffering, and that solved the problem.
1

getLine waits for a newline character to be typed (\n), because what if the user typed a bunch of characters, but never pressed enter? Then it would be an error if some of the "line" had already be processed, if that "line" wasn't a line after all.

You should use getContents instead which will return everything that is typed at the terminal.

Also, you are using the following line:

then hGetContents =<< openFile (head args) ReadMode

This will open a file and never close it. This is fine for your short program, but it might be a better idea for the future to get used to doing this:

then readFile $ head args

1 Comment

The problem doesn't lie with the getLine at the beginning, but rather with the getChar near the end (in the exec function).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.