import Control.Monad import Text.Printf editor :: String -> String editor = uncurry ((++) . reverse) . foldl f ("","") where f (ls,r:rs) ']' = (r:ls,rs) f (ls, "") ']' = (ls, "") f (l:ls,rs) '[' = (ls,l:rs) f ("", rs) '[' = ("", rs) f (ls, rs) x = (x:ls,rs) main :: IO () main = forM_ ["abc[-[[/", "a[b[c]]d", "a[[[[b]]]]]]c"] $ liftM2 (printf "\"%s\" -> \"%s\"\n") id editor