[英]multiple statements are executed when main is called recursively in haskell if-then-else
這是我的代碼
main :: IO ()
main = do
putStr "Enter user name: "
userName <- getLine
putStr "Enter filename: "
fileName <- getLine
contents <- readFile fileName
--------SOME CODES HERE------------------
putStrLn ("TITLES AND RATINGS OF FILMS BY " ++ userName)
-----------SOME CODES HERE-----------------
putStr "ENTER 'Y' TO SAVE CHANGES AND EXIT: "
ex <- getChar
if ex /= 'Y' then
main
else do
writeFile fileName (show film')
exitWith ExitSuccess
當我輸入'Y'的任何邊值時,我得到:
輸入用戶名:輸入文件名:_
而不只是
輸入用戶名: _
有人知道為什么會這樣嗎?
getChar
僅讀取單個字符。 但是您按了Enter鍵,所以您還輸入了一個換行符,當您遞歸調用main
時,第一個getLine
讀取該換行符。
只需使用getLine
代替getChar
並檢查它是否為/= "Y"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.