Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
第4回 スタートHaskell2 数当てゲーム解答
import System.IO
import Data.List
import System.Random
main = do
gen <- getStdGen
let answer = take 4 (randomRs ('1', '4') gen)
putStrLn "Hello, guess the number!"
guessNumber answer
guessNumber::String->IO ()
guessNumber answer = do
putStr "> "
hFlush stdout
line <- getLine
if line == answer
then putStrLn "correct!"
else do
if ((length line) == (length answer))
then do
print $ countSamePos answer line
print $ countSameNum (sort answer) (sort line)
else
putStrLn "input 4 numbers!"
guessNumber answer
countSamePos xs ys = length . filter id $ zipWith (==) xs ys
countSameNum xss@(x:xs) yss@(y:ys)
| x == y = 1 + countSameNum xs ys
| x < y = countSameNum xs yss
| otherwise = countSameNum xss ys
countSameNum _ _ = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment