import Data.Char import Data.List.Split import Data.Maybe import Text.Read sep :: String -> [String] sep s = concat $ map sepBr (splitOn " " s) where sepBr :: String -> [String] sepBr "" = [] sepBr " " = [] sepBr word | a `elem` brackets = [[a]] ++ sepBr bc | c `elem` brackets = sepBr ab ++ [[c]] | otherwise = ([a : bc]) where a = if word == [] then [] else head word b = if word == [] then [] else init $ tail word c = if word == [] then [] else last word ab = [a] ++ b bc = b ++ [c] brackets = ['(', ')'] main :: IO() main = do putStrLn $ show $ sep "(+ 12 (* 3 4 (+ 1 2 3) 5) 1)"