I'm dipping my toe into the Haskell pool, and I'm starting to get the hang of it. For the most part, the lack of traditional control structures doesn't bother me too much. (I'm coming from a C/C++ background.) But I'm a little confused about how you'd repeat an action. For example, if you have a turn-based game, in an imperative language, you might do something like this:
while (not somePlayerWon())
{
getNextMove();
updateGameState();
}
It's not clear to me how you'd do this in Haskell. You could do something recursive like:
playARound gameState = do
nextMove <- getNextMove gameState
newGameState <- updateGameState gameState nextMove
if (not somePlayerWon newGameState)
playARound newGameState
else gameOver -- I realize this probably has to return something
But if you do that, don't you run the risk of a stack overflow? Or will the compiler take the tail-recursive definition and convert it into the equivalent of a for
loop? If so, is this the accepted way of doing this sort of thing?