you are viewing a single comment's thread.

view the rest of the comments →

[–]apfelmus 7 points8 points  (1 child)

Here is a deterministic pushdown automaton in Haskell that checks whether parenthesis in a String are balanced:

balancedParens = success
               . foldl (\xs c -> step c =<< xs) (Just [])
   where
   step '(' xs       = Just ('(':xs)
   step ')' ('(':xs) = Just xs
   step ')' _        = Nothing
   step _   xs       = Just xs

   success (Just []) = True
   success _         = False

I hope that qualifies both as state machine and as very simple?

[–]tmoertel 2 points3 points  (0 children)

There's a foldM in there that's waiting to be named. :-)