Search code examples
haskelliobuffering

Why isn't my IO executed in order?


I got a problem with IO not executing in order, even inside a do construct.

In the following code I am just keeping track of what cards are left, where the card is a tuple of chars (one for suit and one for value) then the user is continously asked for which cards have been played. I want the putStr to be executed between each input, and not at the very end like it is now.

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

Solution

  • absz's answer is correct, Haskell's buffered IO is what's causing you trouble. Here's one way to rewrite your doLoop to have the effect you're looking for:

    doLoop xs = do  putStrLn $ show xs
                    input <- getLine
                    let s:n:_ = input
                    doLoop $ remove (s,n) xs
    

    The two changes: use putStrLn to append a newline and flush the output (which is probably what you want), and use getLine to grab the input a line at a time (again, probably what you want).