Created
October 14, 2016 07:42
-
-
Save naldoco/c3758b408c6472595decb4eed8c2b2c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- check "hola" "---a" 'b' = (False, "---a") | |
check :: String -> String -> Char -> (Bool,String) | |
check word display c = | |
(c `elem` word, [if x == c | |
then c | |
else y | (x, y) <- zip word display]) | |
mkguess :: String -> String -> Int -> IO () | |
mkguess word display n = | |
do putStrLn (display ++ " " ++ take n (repeat '*')) | |
if n == 0 then putStrLn ("You lose. " ++ word) | |
else do | |
putStr " Enter your guess: " | |
q <- getLine | |
let (correct, display') = check word display (q!!0) | |
let n' = if correct then n else n-1 | |
if word == display' then putStrLn "You win!" | |
else mkguess word display' n' | |
starman :: String -> Int -> IO () | |
starman word n = mkguess word ['-' | _ <- word] n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All the hard work is done by 'check', so it's important to keep it pure (no IO). The interactive part is done by 'mkguess', so it must be impure. And finally it is good to have an initialization function, for example to read a data file at the start of the program (also impure) ' starman' is a good place. And that's all. Only two impure functions.
Look at the signature of 'turn' and 'mkguess'. It's the same. It's easy to combine them!
See: https://www.futurelearn.com/courses/functional-programming-haskell/1/steps/116475/comments#fl-comments