Hangman in Haskell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import System.IO
import Data.List
import Data.Char

display word = intersperse ' ' $ [if x `elem` ['a'..'z'] then '_' else x | x <- word]

makeGuess word letter guesses
	| letter `elem` word	 = play [if letter == x then (toUpper letter) else x | x <- word] guesses
	| otherwise				 = play word (guesses - 1)
								 
play word guesses
	| word == (map toUpper word) = do
		putStrLn $ display word
		putStrLn $ "You Win!"
	| guesses == 0 = do
		putStrLn $ display word
		putStrLn $ "You Lose..."
	| otherwise = do
		putStrLn $ "You have " ++ (show guesses) ++ " guesses left."
		putStrLn $ display word
		putStr $ "Guess a letter: "
		hFlush stdout
		userGuess <- getLine
		makeGuess word (head userGuess) guesses
	
main = do
	putStrLn $ "Welcome to Haskell Hangman!"
	putStrLn $ "Enter a word to guess"
	userInput <- getLine -- Get a string from user and store it in userInput
	play (map toLower userInput) 6
	putStrLn $ "Thanks for playing!"
	
5:16: Warning: Redundant $
Found:
intersperse ' ' $
[if x `elem` ['a' .. 'z'] then '_' else x | x <- word]
Why not:
intersperse ' '
[if x `elem` ['a' .. 'z'] then '_' else x | x <- word]
8:42: Warning: Redundant bracket
Found:
if letter == x then (toUpper letter) else x
Why not:
if letter == x then toUpper letter else x
12:11: Warning: Redundant bracket
Found:
word == (map toUpper word)
Why not:
word == map toUpper word
14:17: Warning: Redundant $
Found:
putStrLn $ "You Win!"
Why not:
putStrLn "You Win!"
17:17: Warning: Redundant $
Found:
putStrLn $ "You Lose..."
Why not:
putStrLn "You Lose..."
19:43: Warning: Redundant bracket
Found:
(show guesses) ++ " guesses left."
Why not:
show guesses ++ " guesses left."
21:17: Warning: Redundant $
Found:
putStr $ "Guess a letter: "
Why not:
putStr "Guess a letter: "
27:9: Warning: Redundant $
Found:
putStrLn $ "Welcome to Haskell Hangman!"
Why not:
putStrLn "Welcome to Haskell Hangman!"
28:9: Warning: Redundant $
Found:
putStrLn $ "Enter a word to guess"
Why not:
putStrLn "Enter a word to guess"
31:9: Warning: Redundant $
Found:
putStrLn $ "Thanks for playing!"
Why not:
putStrLn "Thanks for playing!"

Hangman in Haskell (annotation)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- Remarks CKoenig: I removed reduntant $, (), .., renamed some identifiers and added type-def.s
import System.IO
import Data.List
import Data.Char

display :: String -> String
display word = intersperse ' ' [if c `elem` ['a'..'z'] then '_' else c | c <- word]

makeGuess :: String -> Char -> Int -> IO ()
makeGuess word letter guesses
	| letter `elem` word	 = play [if letter == c then toUpper letter else c | c <- word] guesses
	| otherwise				 = play word (guesses - 1)
								 
play :: String -> Int -> IO ()						 
play word guesses
	| word == map toUpper word = do
		putStrLn $ display word
		putStrLn "You Win!"
	| guesses == 0 = do
		putStrLn $ display word
		putStrLn "You Lose..."
	| otherwise = do
		putStrLn $ "You have " ++ show guesses ++ " guesses left."
		putStrLn $ display word
		putStr "Guess a letter: "
		userGuess <- getLine
		makeGuess word (head userGuess) guesses
	
main :: IO()
main = do
	hSetBuffering stdout NoBuffering
	putStrLn "Welcome to Haskell Hangman!"
	putStrLn "Enter a word to guess"
	userInput <- getLine -- Get a string from user and store it in userInput
	play (map toLower userInput) 6
	putStrLn "Thanks for playing!"