【发布时间】:2026-02-22 19:30:01
【问题描述】:
我遇到了一个问题,即 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
【问题讨论】: