Last active
December 26, 2015 13:19
-
-
Save safx/7157736 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
score' :: Int -> Char -> Int | |
score' _ '-' = 0 | |
score' b '/' = 10 - b | |
score' _ 'X' = 10 | |
score' _ '1' = 1 | |
score' _ '2' = 2 | |
score' _ '3' = 3 | |
score' _ '4' = 4 | |
score' _ '5' = 5 | |
score' _ '6' = 6 | |
score' _ '7' = 7 | |
score' _ '8' = 8 | |
score' _ '9' = 9 | |
score = score' 0 | |
calc :: Int -> [Char] -> Int | |
calc 10 ('X':'X':x:[]) = 20 + (score x) | |
calc 10 ('X':x:y:[]) = 10 + (score x) + (score y) | |
calc 10 (x:'/':y:[]) = 10 + (score y) | |
calc 10 (x:y:[]) = (score x) + (score y) | |
calc f ('X':xs) = 10 + b1 + b2 + (calc (f+1) xs) | |
where b1 = score (head xs) | |
b2 = score' b1 (head (tail xs)) | |
calc f (x:'/':xs) = 10 + b1 + (calc (f+1) xs) | |
where b1 = score (head xs) | |
calc f (x:y:xs) = t1 + t2 + (calc (f+1) xs) | |
where t1 = score x | |
t2 = score' t1 y | |
printTest (testCase, result) = do | |
putStrLn $ (if (calc 1 testCase) == result then "CLEAR" else "FAIL") ++ " " ++ testCase | |
testCases = [ | |
("9-9-9-9-9-9-9-9-9-9-", 90), | |
("X54----------------", 28), | |
("1/5-----------------", 20), | |
("1/5-2/-/8-----------", 56), | |
("------XX----------", 30), | |
("------XXX--------", 60), | |
("XXXXXXXXXXXX", 300), | |
("--------------------", 0), | |
("-------------------/5", 15), | |
("------------------X54", 19), | |
("5/5/5/5/5/5/5/5/5/5/5", 150) | |
] | |
test = mapM_ printTest testCases | |
main = test | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment