Created
May 27, 2012 09:07
-
-
Save sumerman/2802975 to your computer and use it in GitHub Desktop.
Solution for a classic balanced-parens problem
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
{- | |
Bal ::= (Bal)Bal | [Bal]Bal | "" | |
-} | |
import Control.Monad | |
brackets = [('[',']'), ('(',')')] | |
isObr b = elem b (map fst brackets) | |
isCoBr b1 b2 = elem (b1,b2) brackets || elem (b2,b1) brackets | |
balanced (x:xs) | isObr x = | |
balanced >=> closing x >=> balanced $ xs | |
where | |
closing x (y:ys) | isCoBr x y = return ys | |
closing _ _ = mzero | |
balanced x = return x | |
check = maybe False null . balanced | |
main = do | |
s <- getLine | |
print $ check s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment