我有一系列网络请求,每个请求大于10秒.
为了让用户知道发生了什么,我给出了更新:
main = do putStr "Downloading the first thing... "
{- Net request -}
putStrLn "DONE"
putStr "Downloading the second thing... "
{- Net request -}
putStrLn "DONE"
Run Code Online (Sandbox Code Playgroud)
使用GHCi,这可以按预期工作,但编译或使用runghc,"下载"不打印直到"完成".
我用(>> =)和(>>)重写了它,但是我遇到了同样的问题.
这是怎么回事?
我遇到的问题是IO没有按顺序执行,即使在do构造中也是如此.
在下面的代码中,我只是跟踪剩下的卡片,卡片是一个字符元组(一个用于套装,一个用于值)然后用户不断被问到已经播放了哪些卡片.我希望putStr在每个输入之间执行,而不是像现在这样在最后执行.
module Main where
main = doLoop cards
doLoop xs = do putStr $ show xs
s <- getChar
n <- getChar
doLoop $ remove (s,n) xs
suits = "SCDH"
vals = "A23456789JQK"
cards = [(s,n) | s <- suits, n <- vals]
type Card = (Char,Char)
remove :: Card -> [Card] -> [Card]
remove card xs = filter (/= card) xs
Run Code Online (Sandbox Code Playgroud)