wvogel日記

自分用の技術備忘録が多めです.

第二問

括弧がバランスしていればyes,そうでなければnoを出力

main = getContents
     >>= (\x->putStr $ unlines $map (judge.scope) $lines x)

judge :: [Bool] -> String
judge ks = if last ks == True then "yes\n" else "no\n"

scope :: String -> [Bool]
scope [] = [True]
scope (x:xs) | x == '(' = scope' ')' xs
             | x == '[' = scope' ']' xs
             | otherwise = scope xs
    where
        scope' c [] = [False]
        scope' c (x:xs) | c == x = True:scope xs
                        | x == '(' = scope' ']' xs
                        | x == '[' = scope' ']' xs
                        | otherwise = scope' c xs

本来はピリオド入力で終了条件となるのですが、その辺はとりあえず置いておいて。
Parsecを使えばもっと簡単だったでしょうが。