fork(2) download
  1. tokenize "" = ["epsilon"]
  2. tokenize (':':x:xs) =
  3. case x of
  4. ')' -> "smiley" : (tokenize xs)
  5. '(' -> "frowney" : (tokenize xs)
  6. _ -> "ignored" : (tokenize xs)
  7.  
  8.  
  9. tokenize ('(':xs) = "left_paren" : (tokenize xs)
  10. tokenize (')':xs) = "right_paren" : (tokenize xs)
  11. tokenize (x:xs) = "ignored" : (tokenize xs)
  12.  
  13. parseParens stack (tok:tokens) =
  14. case tok of
  15. "left_paren" -> parseParens ("left_paren":stack) tokens
  16. "right_paren" ->
  17. case stack of
  18. (s:[]) -> parseSentence True tokens
  19. (s:st) -> parseParens st tokens
  20. "epsilon" -> parseSentence False []
  21. "smiley" -> case stack of
  22. ("left_paren":[]) -> parseSentence True tokens
  23. _ -> parseParens stack tokens
  24. "frowney" -> parseParens stack tokens
  25. _ -> parseParens stack tokens
  26.  
  27. parseSentence False [] = False
  28. parseSentence True [] = True
  29.  
  30. parseSentence intable (tok:tokens) =
  31. case tok of
  32. "ignored" -> parseSentence True tokens
  33. "smiley" -> parseSentence True tokens
  34. "frowney" -> parseSentence True tokens
  35. "epsilon" -> parseSentence True tokens
  36. "left_paren" -> parseParens ["left_paren"] tokens
  37. "right_paren" -> False
  38.  
  39. isBalanced = parseSentence True . tokenize
  40.  
  41. yesno True = "YES"
  42. yesno False = "NO"
  43.  
  44. out n xs = "Case #"++(show n)++": "++(show $ yesno $ isBalanced xs)
  45.  
  46. parseInput xs =
  47. let cases = tail $ lines xs in
  48. unlines $ map (uncurry out) (zip [1..] cases)
  49.  
  50. main = do
  51. -- input <- readFile "./emotinput2"
  52. -- writeFile "./emotoutput2" $ parseInput input
  53. print $ isBalanced "((:)))"
Success #stdin #stdout 0.01s 3588KB
stdin
Standard input is empty
stdout
True