2013-03-15 79 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

相关问题