Created
September 4, 2012 12:49
-
-
Save willnet/3620877 to your computer and use it in GitHub Desktop.
http://qiita.com/items/ebd8a56b41711ba459f9 の問題をhaskellで解いてみた
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 Data.Char | |
import System.Environment | |
main = do | |
args <- getArgs | |
print . process. head $ args | |
process = foldl (\acc x -> cal acc x) [] | |
cal acc x = acc ++ [r x] | |
where | |
now = if acc == [] then [] else last acc | |
r 's' = strike now | |
r 'b' = ball now | |
r 'h' = hit now | |
r 'p' = pitcher now | |
r 'f' = foul now | |
strike :: String -> String | |
strike [] = strike "000" | |
strike (o:s:b) | |
| si < 2 = o : intToDigit(si + 1) : b | |
| si >= 2 = if oi == 2 then "000" else intToDigit (oi + 1): "00" | |
where | |
si = digitToInt s | |
oi = digitToInt o | |
ball :: String -> String | |
ball [] = ball "000" | |
ball (o:s:b) | |
| bi < 3 = o:s:intToDigit(bi + 1): [] | |
| bi == 3 = o: "00" | |
where | |
bi = digitToInt . head $ b | |
hit :: String -> String | |
hit [] = hit "000" | |
hit (o:_) = o: "00" | |
pitcher :: String -> String | |
pitcher [] = pitcher "000" | |
pitcher (o:s:b) | |
| oi < 2 = intToDigit(oi + 1) : "00" | |
| oi == 2 = "000" | |
where | |
oi = digitToInt o | |
foul :: String -> String | |
foul [] = "000" | |
foul x@(o:s:b) | |
| si < 2 = o : intToDigit(si + 1) : b | |
| si == 2 = x | |
where | |
si = digitToInt s | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment