-- Hangman game, Lecture 3A -- 2014-11-17 -- Dave Sands -- Earlier versions (see Hangman-2012.hs) are less structured -- Here we illustrate a clean separation between IO code and -- pure game logic import GameLogic import Data.List import System.Random(randomRIO) dict = "/usr/share/dict/words" -- mac OS X main :: IO() main = do w <- getWord gameLoop (createState w) gameLoop :: State -> IO() gameLoop s | lost s = outcome $ "The word was " ++ word s ++ " - you lost!" | won s = outcome "You won" | otherwise = do putStr $ gameState s putStr "Guess one or more chars:" newguess <- getLine gameLoop $ addGuess newguess s where outcome o = putStrLn $ gameState s ++ o getWord :: IO String getWord = do s <- readFile dict let ws = words s n <- randomRIO (0,length ws -1) return $ ws !! n