Hi. I'm learning Haskell from an on-line tutorial, and trying to figure out how I/O works. I'm a bit confused with this example:
main = forever $ do
putStr "Next line: "
l <- getLine
putStrLn $ map toUpper l
My expectation is for it to ask me for a line, then for it to convert the line I provide to uppercase (err... create a new line that is uppercase) and then output said line.
However, what actually happens when I run it is it firsts ask me for a line, then outputs "Next line: ", then outputs the uppercase line:
$ ghc forever-example.hs -o forever-example
Next line: HI THERE
More of my input
Next line: MORE OF MY INPUT
and so on
Next line: AND SO ON
I realize that Haskell is not an imperative language, but I was (mistakenly?) under the impression that there was some kind of sequence guaranteed in the do blocks. Could someone explain why this happens, and how I can "fix" my code to get the desired behavior?
- Linux version 2.6.34-gentoo-r12 (64-bit)
- GNU bash, version 4.1.9(2)-release (x86_64-pc-linux-gnu)
- Glasgow Haskell Compiler, Version 6.12.3, for Haskell 98, stage 2 booted by GHC version 6.12.3