Course:CPSC312-2017-Hangman Haskell

From UBC Wiki


Authors: Yan, Derek

What is the problem?

We will create a game in Haskell that follows the rules of Hangman. Our github is here:

What is the something extra?

Hangman requires the program the track the number of incorrect answers to display the image of the hangman, and functional programming should not have mutable variables. Thus we cannot rely on a global variable the same way we do for Java and other languages.

What did we learn from doing this?

While Haskell is fully capable of handling this task, we had difficulties storing state and input, so we had to rely on using monads. Without this, we would have had to give every single case possible, which is tedious and does not scale. We stored the guesses like so:

 data State = State 
   { guessesLeft :: Int
   , guessedChars :: String
   , targetWord :: String }

Masking the input required system IO access to UNIX's stdin and stdout. We learned how to contain the game's state in a monad and pass it around in a loop, updating it as the game changes which is basically how our whole game runs. We also learned that IO () is like a contagious disease and that the moment you use it in one function, every function it touches also becomes IO.