1.问题描述
在使用ghc编译haskell程序后,getLine总是在putStr之前执行,尽管putStr放在了前面。这时只需要刷新IO,便可解决此问题。
2.解决方案
import System.IO
main = do
putStr "Input file: "
hFlush stdout --刷新标准输出
ifile <- getLine
putStrLn ifile
putStr "Output file: "
hFlush stdout
ofile <- getLine
putStrLn ofile
3.编译程序
$ ghc -o example example.hs
4.运行及其结果
$ ./example
Input file: test1
test1
Output file: test2
test2