fork download
  1. import Control.Monad
  2. import Text.Printf
  3.  
  4. editor :: String -> String
  5. editor = uncurry ((++) . reverse) . foldl f ("","")
  6. where
  7. f (ls,r:rs) ']' = (r:ls,rs)
  8. f (ls, "") ']' = (ls, "")
  9. f (l:ls,rs) '[' = (ls,l:rs)
  10. f ("", rs) '[' = ("", rs)
  11. f (ls, rs) x = (x:ls,rs)
  12.  
  13. main :: IO ()
  14. main = forM_ ["abc[-[[/", "a[b[c]]d", "a[[[[b]]]]]]c"]
  15. $ liftM2 (printf "\"%s\" -> \"%s\"\n") id editor
  16.  
Success #stdin #stdout 0s 6312KB
stdin
Standard input is empty
stdout
"abc[-[[/" -> "a/b-c"
"a[b[c]]d" -> "cbad"
"a[[[[b]]]]]]c" -> "bac"