1

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.

sdcvvc
  • 25,343
  • 4
  • 66
  • 102
Optimight
  • 2,989
  • 6
  • 30
  • 48
  • "Because there is no newline" :P~ (Now who's responsible for it .. is the question.) –  Aug 02 '12 at 00:37
  • @pst: So, I have to just accept it as it is or ? – Optimight Aug 02 '12 at 00:44
  • Well, you could always edit the .el, or there might be a setting .. I don't use haskell (or related modes), though :-) Other approaches might be to just put a newline char (\n should suffice) or force it through putStrLn. –  Aug 02 '12 at 00:58

2 Answers2

3

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.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
2

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> 
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154