Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List (replicate)
- main = getContents >>= (\x -> putStr (proc 0 x))
- space = ' '
- newline = '\n'
- isOpening = \c -> case c of
- '[' -> True
- '{' -> True
- '(' -> True
- _ -> False
- isClosing = \c -> case c of
- ']' -> True
- '}' -> True
- ')' -> True
- _ -> False
- isSeparator = \c -> case c of
- ';' -> True
- ',' -> True
- _ -> False
- isStringOpening = \c -> case c of
- '"' -> True
- _ -> False
- indentation = flip replicate space . (2 *)
- proc :: Int -> String -> String
- proc indent = \s -> case s of
- x : xs
- | isOpening x ->
- let indent' = succ indent in
- newline : indentation indent ++ x : newline : indentation indent' ++ proc indent' xs
- | isClosing x ->
- let indent' = pred indent in
- newline : indentation indent' ++ x : proc indent' xs
- | isSeparator x ->
- x : newline : indentation indent ++ proc indent (dropWhile (== space) xs)
- | isStringOpening x ->
- let (str, rest) = break (== x) xs in
- x : str ++ take 1 rest ++ proc indent (drop 1 rest)
- | otherwise ->
- x : proc indent xs
- _ -> ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement