我不明白为什么这段代码只循环一次然后退出?在Ghci我只能回答第一个循环,然后似乎变量cont设置为false,我没有提示回答.
结果是:
*Main> testLoop1 td10
test
Do you want to continue? (y/N)
y
we continue
test
Do you want to continue? (y/N)
We stop
Run Code Online (Sandbox Code Playgroud)
码:
type TDeckSTIO = StateT TableDecks IO
continue = do
putStrLn "Do you want to continue? (y/N)"
c <- getChar
return $ c == 'y'
loop1 :: TDeckSTIO ()
loop1 = do
liftIO $ putStrLn "test"
cont<- liftIO continue
if cont
then do
liftIO $ putStrLn "we continue"
liftIO $ testLoop1 td
else liftIO $ putStrLn "We stop"
testLoop1 td = runStateT (loop1 ) td >> return ()
Run Code Online (Sandbox Code Playgroud)
ehi*_*ird 15
问题是当你输入y并按Enter键时,实际上输入了两个字符:'y'本身,以及通过按返回键发送的换行符.第一次循环,循环看到'y',但下一次,它看到'\n',并且'\n'从不'y',它退出.
您可以hSetBuffering stdin NoBuffering在进入循环之前执行(您需要导入System.IO),这样您可以在不等待换行符的情况下处理字符,或者一次专门处理行:
continue = do
putStrLn "Do you want to continue? (y/N)"
s <- getLine
return $ s == "y"
Run Code Online (Sandbox Code Playgroud)
顺便说一下,liftIO $ testLoop1 td你可以保持相同的状态monad 而不是写作:你可以替换它,loop1它将完全相同.
另外,testLoop1写得更好:
testLoop1 = evalStateT loop1
Run Code Online (Sandbox Code Playgroud)
evalStateT就像runStateT,但不包括最终状态,因此您不必明确地丢弃该值>> return ().
| 归档时间: |
|
| 查看次数: |
159 次 |
| 最近记录: |