当我在Haskell中使用
这样做是否正确?我做错了什么吗? 示例1:
putStrLn txt
从多个线程中输出文本时,可能会出现换行符与文本交错的情况,但如果我使用putStr $ txt ++ "\n"
则总是正常工作。这样做是否正确?我做错了什么吗? 示例1:
thread 1: putStrLn "txt 1"
thread 2: putStrLn "txt 2"
thread 3: putStrLn "txt 3"
thread 4: putStrLn "txt 4"
thread 5: putStrLn "txt 5"
输出示例:
txt 1txt 3
txt 2txt 5txt 4
例子2:
thread 1: putStr $ "txt 1" ++ "\n"
thread 2: putStr $ "txt 2" ++ "\n"
thread 3: putStr $ "txt 3" ++ "\n"
thread 4: putStr $ "txt 4" ++ "\n"
thread 5: putStr $ "txt 5" ++ "\n"
始终为线程输出一行:
txt 1
txt 3
txt 2
txt 5
txt 4
谢谢
更新:
我正在使用ghc 6.12.3
和base-4.2.0.2
base 4.3.1.0
中已经更改了。在base 4.3.0.0
及之前,它的定义为putStrLn s = do putStr s; putChar '\n'
。 - hammar