Skip to content

Instantly share code, notes, and snippets.

@willnet
Created September 4, 2012 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willnet/3620877 to your computer and use it in GitHub Desktop.
Save willnet/3620877 to your computer and use it in GitHub Desktop.
http://qiita.com/items/ebd8a56b41711ba459f9 の問題をhaskellで解いてみた
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