嘿-伟大的程序员和haskellers,我是haskell的大一新生,对一个程序有问题,归根结底是以下情况
main :: IO ()
main = do
putStrLn "\nplease give me some input"
input1 <- getLine
putStrLn "\nplease give me another input"
input2 <-getLine
putStrLn ("\nyour inputs were "++show(input1)++" and "++ show(input2)")
putStrLn "restart ?? yY or nN"
c <- getChar
restart c
where
restart c
|elem c "yY" = do
main
|elem c "nN" = putStrLn "\nExample Over"
|otherwise = do
putStrLn "\nyou must type one of Yy to confirm or nN to abort"
c'<- getChar
restart c'在除第一次执行main之外的任何一次
input1 <- getLine被跳过,并且我找不到任何原因,如下所示
input2 <- getLine按预期执行,我愿意接受任何建议和帮助,提前感谢ε/2
发布于 2011-04-10 00:00:24
修复方法:在程序开始时设置NoBuffering:
hSetBuffering stdin NoBuffering为什么这样做可以解决这个问题?当你不使用NoBuffering时,看看你输入的是什么!你输入,getLine就会消耗:
first input[enter]然后你输入,getLine #2消耗:
second input[enter]然后键入:
y[enter]但是getChar只使用了y,而留下了缓冲的[enter],这是您的第一个getLine调用所读取的!为什么要键入[enter]?因为你必须这样做,只是点击'y‘并不会导致main循环,因为终端是行缓冲的。
https://stackoverflow.com/questions/5605983
复制相似问题