I'm not sure I understand what you mean by "I know you have the best intentions in writing this, but there are pitfalls.". Anyway, here's the code which doesn't work apparently because mapM is waiting for the whole list before it goes further.
prompt = ">> "
commands :: [IO String]
commands = readCommand : commands
where readCommand = putStr prompt >> getLine
display :: Show a => [ a ] -> IO ()
display = mapM_ $ putStr . show
executeCommand :: String -> String
executeCommand = printf "Command not implemented: '%s'"
processCommands :: [IO String] -> IO [ String ]
processCommands = mapM processOneCommand
where processOneCommand cmd = cmd >>= (return . executeCommand )
main =
hSetBuffering stdout NoBuffering
>> processCommands commands
>>= display
This is just for learning purposes and I'm looking for the "haskell way to do it". My intention is to write the function processCommands such that it takes the decision to either fetch the next command from the command list (i.e. console) or to exit the application.
Regarding your comment "Just know that at some point you should learn to use conduits or pipes for a much better approach to modeling things like this.". Can you point me to some documentation?
Thanks!