Created
September 9, 2012 08:19
-
-
Save kawakami-o3/3683302 to your computer and use it in GitHub Desktop.
Start Haskell2 - Exercise 9
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
import System.Random | |
import System.IO (hFlush, stdout) | |
exactNum :: String -> String -> Int | |
exactNum a b = exactNum' 0 a b | |
where | |
exactNum' n [] [] = n | |
exactNum' n (a:as) (b:bs) = exactNum' (if a==b then (n+1) else n) as bs | |
matchNum :: String -> String -> Int | |
matchNum = matchNum' 0 | |
where | |
matchNum' n _ [] = n | |
matchNum' n a (b:bs) = matchNum' (if (matchChar b a) then (n+1) else n) a bs | |
matchChar :: Char -> String -> Bool | |
matchChar _ [] = False | |
matchChar c (x:xs) | c == x = True | |
| otherwise = matchChar c xs | |
lookUp :: String -> IO () | |
lookUp correct = do | |
putStr "> " | |
hFlush stdout | |
x <- getLine | |
if x == correct | |
then putStrLn "correct" | |
else do | |
putStrLn $ ((show (exactNum correct x)) ++ " " ++ (show (matchNum correct x))) | |
lookUp correct | |
main = do | |
gen <- getStdGen | |
let correct = take 4 (randomRs ('0','9') gen) | |
putStrLn correct | |
lookUp correct |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://wiki.haskell.jp/Workshop/StartHaskell2/exercise9