[Haskell-cafe] unexpected compiler behavior
Donald Bruce Stewart
dons at cse.unsw.edu.au
Tue Dec 5 06:15:49 EST 2006
jepalomar23:
> Hi,
> I am newbie in Haskell and do not understand why the interpreted mode
> differs from the compiled program.
> I run this code
> main = do
> putStr "line1"
> x<-getLine
> putStr "line2"
> y<-getLine
> return (x++y)
>
> either under runhugs or runghc or in a console under ghci and works
> properly, that is, first displays "line1" and then prompts for the
> corresponding inputs through the keyboard, and so on. However, if I
> compile the code with the command line
> ghc --make Main.hs -o main
> and launch the compiled program main, then at first prompts for the
> input lines through the keyboard and then displays the strings "line1"
> "line2".
> I have read in the tutorials that the command "do" implies an ordered
> execution and this only occurs in interpreted mode.
> Using the monadic notation >> , >>= occurs the same unordered behavior.
> ?How can I avoid the unordered execution of this code in a compiled
> program?
Sounds like the difference in buffering between the interactive
environments and compiled code.
Try:
import System.IO
main = do
hSetBuffering stdout NoBuffering
putStr "line1 "
x <- getLine
putStr "line2 "
y <- getLine
print (x++y)
Running this:
$ ./a.out
line1 1
line2 2
"12"
-- Don
More information about the Haskell-Cafe
mailing list