我期望的一小段代码,因为我正在使用 char get/put 方法应该响应每个输入字符,但情况似乎并非如此;而是等待换行符,然后运行整个字符串,这对我来说完全没有意义......
我究竟做错了什么?我如何让它对每个输入字符做出反应?
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>