我在 Haskell 中构建了一个非常简单的 read-eval-print-loop 来捕获 Control-C(UserInterrupt)。但是,每当我编译和运行这个程序时,它总是会捕获第一个 Control-C 并总是在第二个 Control-C 上中止,退出代码为 130。无论我在两者之前和之间输入多少行输入都无关紧要Control-Cs,它总是以这种方式发生。我知道我一定遗漏了一些简单的东西......请帮忙,谢谢!
注意:这是基于 4 的异常,所以是 Control.Exception 而不是 Control.OldException。
import Control.Exception as E
import System.IO
main :: IO ()
main = do hSetBuffering stdout NoBuffering
hSetBuffering stdin NoBuffering
repLoop
repLoop :: IO ()
repLoop
= do putStr "> "
line <- interruptible "<interrupted>" getLine
if line == "exit"
then putStrLn "goodbye"
else do putStrLn $ "input was: " ++ line
repLoop
interruptible :: a -> IO a -> IO a
interruptible a m
= E.handleJust f return m
where
f UserInterrupt
= Just a
f _
= Nothing