Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import Data.List (replicate)
  2.  
  3. main = getContents >>= (\x -> putStr (proc 0 x))
  4.  
  5. space = ' '
  6. newline = '\n'
  7.  
  8. isOpening = \c -> case c of
  9. '[' -> True
  10. '{' -> True
  11. '(' -> True
  12. _ -> False
  13.  
  14. isClosing = \c -> case c of
  15. ']' -> True
  16. '}' -> True
  17. ')' -> True
  18. _ -> False
  19.  
  20. isSeparator = \c -> case c of
  21. ';' -> True
  22. ',' -> True
  23. _ -> False
  24.  
  25. isStringOpening = \c -> case c of
  26. '"' -> True
  27. _ -> False
  28.  
  29. indentation = flip replicate space . (2 *)
  30.  
  31. proc :: Int -> String -> String
  32. proc indent = \s -> case s of
  33. x : xs
  34. | isOpening x ->
  35. let indent' = succ indent in
  36. newline : indentation indent ++ x : newline : indentation indent' ++ proc indent' xs
  37. | isClosing x ->
  38. let indent' = pred indent in
  39. newline : indentation indent' ++ x : proc indent' xs
  40. | isSeparator x ->
  41. x : newline : indentation indent ++ proc indent (dropWhile (== space) xs)
  42. | isStringOpening x ->
  43. let (str, rest) = break (== x) xs in
  44. x : str ++ take 1 rest ++ proc indent (drop 1 rest)
  45. | otherwise ->
  46. x : proc indent xs
  47. _ -> ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement