haskell-mode prints "*Main>" on same line after using putChar?

237 views Asked by At

Code:

main = do
  putChar 't'
  putChar 'e'
  putChar 'h'

While I run above-mentioned code, I am getting

*Main> main
teh*Main>

But I am expecting

*Main> main
teh
*Main>

My question: Why teh*Main comes instead of teh and then *Main in another line?

I am using emacs23 with haskell-mode.

2

There are 2 answers

0
Daniel Fischer On BEST ANSWER

putChar c writes just that one character to the console. That's what it's intended for. So unless you print a newline to the console afterward, whether with putChar, putStr or whatever other methods, the following output goes to the same line. The behaviour is the same as with C, or if you cat a file without trailing newline. It's ubiquitous. The only feasible alternative (it's unfeasible to check each output whether it ended with a newline) is to output a newline before the ghci or shell prompt unconditionally, which would lead to many annoying blank lines.

0
Chris Taylor On

If it really bothers you, you could always define

putCharLn :: Char -> IO ()
putCharLn c = do putChar c
                 putChar '\n'

Define main like:

main = do putChar 't'
          putChar 'e'
          putCharLn 'h'

And now:

*Main> main
teh
*Main>