2013-03-15 85 views
0

因爲我使用char get/put方法應該對每個輸入字符作出響應,所以我期望得到一小段代碼,但是似乎並非如此;而是等待換行符,然後運行整個字符串,這對我來說並不合理......爲什麼我的字符回顯等待換行符,回顯整個字符串而不是char-for-char

我在做什麼錯?我如何得到這個對每個輸入字符作出反應?

main = repeatUntilExit stdin stdout putChar "" 

repeatUntilExit :: Handle -> Handle -> (Char -> IO()) -> [Char] -> IO() 
repeatUntilExit hIn hOut f "exit\n" = hPutStrLn hOut "bye\n" 
repeatUntilExit hIn hOut f x = hGetChar hIn >>= \c -> f c >> repeatUntilExit hIn hOut f (appendToLastFive c) 
    where appendToLastFive a = (reverse . (:)a . take 4 . reverse) x 

結果是:

C:\temp>echo.exe 
an entire line of text 
an entire line of text 
exit 
exit 
bye 

C:\temp> 

回答

5

這是一個buffering問題。

import System.IO 

hSetBuffering stdin NoBuffering 

然而,這並不適用於Windows,仍然是一個未定義的問題,注意到here

+0

感謝所有幫助戴夫,你只是最近比任何人都更好地馴服haskell標籤 – 2013-03-15 15:32:08

+0

在查看haskell標籤之前,您有無與倫比的能力發佈問題。這是相當混亂。 – dave4420 2013-03-15 15:35:17

+0

@JimmyHoffa重新編輯:這是不幸的:-( – dave4420 2013-03-15 15:39:36

相關問題