3 ms·
My naive version would be import Control.Monad (foldM) checkBalanced :: [Char] -> Bool checkBalanced = isStackEmpty . foldM step mempty where
by Tarean 8y ago
My naive version would be
import Control.Monad (foldM)
checkBalanced :: [Char] -> Bool
checkBalanced = isStackEmpty . foldM step mempty
where
step (x:xs) c
| x == c = Just xs
step ls c
| Just r <- inverse c = Just (r : ls)
step _ _ = Nothing
inverse = flip lookup [( '(', ')' )]
isStackEmpty stack = stack == Just []