Grp Type and filt function.
Chris Pressey
10 years ago
1 | 1 | |
2 | 2 | import System.Environment |
3 | 3 | |
4 | data Type = Word | Line | Para | |
5 | deriving (Show, Ord, Eq) | |
6 | ||
4 | 7 | data Text = Char String |
5 | | Word Text | |
6 | | Line Text | |
7 | | Para Text | |
8 | | Grp Type Text | |
8 | 9 | | Seq [Text] |
9 | 10 | deriving (Show, Ord, Eq) |
10 | 11 | |
15 | 16 | whitespace _ = False |
16 | 17 | |
17 | 18 | |
19 | notEmptyWord (Grp Word (Seq [])) = False | |
20 | notEmptyWord _ = True | |
21 | ||
22 | notChar c (Char d) = not (c == d) | |
23 | notChar c _ = True | |
24 | ||
25 | ||
26 | -- could be implemented as the Show... | |
27 | ||
28 | render (Char s) = s | |
29 | render (Grp Line t) = (render t) ++ "\n" | |
30 | render (Grp Para t) = (render t) ++ "\n" | |
31 | render (Grp Word t) = render t | |
32 | render (Seq []) = "" | |
33 | render (Seq (t:ts)) = (render t) ++ render (Seq ts) | |
34 | ||
35 | ||
18 | 36 | main = do |
19 | 37 | [fileName] <- getArgs |
20 | 38 | run fileName |
21 | 39 | |
22 | 40 | run fileName = do |
41 | a <- readToChars fileName | |
42 | ||
43 | let b = groupCharsToLines a | |
44 | let c = groupLinesToWords b | |
45 | let d = groupLinesToParas c | |
46 | putStr (render d) | |
47 | ||
48 | --let d' = filt notEmptyWord d | |
49 | --putStr (render d') | |
50 | ||
51 | let d' = filt (notChar "a") d | |
52 | putStr (render d') | |
53 | ||
54 | ||
55 | demo fileName = do | |
23 | 56 | a <- readToChars fileName |
24 | 57 | |
25 | 58 | let b = groupCharsToLines a |
59 | 92 | let |
60 | 93 | (line, rest) = scanLine cs [] |
61 | 94 | in |
62 | ((Line $ Seq $ line):(g rest)) | |
95 | ((Grp Line $ Seq $ line):(g rest)) | |
63 | 96 | |
64 | 97 | scanLine [] acc = (reverse acc, []) |
65 | 98 | scanLine (Char "\n":cs) acc = (reverse acc, cs) |
75 | 108 | (word, rest) = scanWord cs [] |
76 | 109 | (spc, rest') = scanSpaces rest [] |
77 | 110 | in |
78 | ((Word $ Seq $ word):(Word $ Seq $ spc):(g rest')) | |
111 | ((Grp Word $ Seq $ word):(Grp Word $ Seq $ spc):(g rest')) | |
79 | 112 | |
80 | 113 | scanWord [] acc = (reverse acc, []) |
81 | 114 | scanWord (c:cs) acc = |
87 | 120 | |
88 | 121 | |
89 | 122 | groupLinesToWords (Seq many) = |
90 | Seq $ map (\(Line x) -> Line $ groupCharsToWords x) many | |
123 | Seq $ map (\(Grp Line x) -> Grp Line $ groupCharsToWords x) many | |
91 | 124 | |
92 | 125 | |
93 | 126 | groupLinesToParas (Seq many) = |
99 | 132 | (lines, rest) = scanPara cs [] |
100 | 133 | (spc, rest') = scanBlankLines rest [] |
101 | 134 | in |
102 | ((Para $ Seq $ lines):(g rest')) | |
135 | ((Grp Para $ Seq $ lines):(g rest')) | |
103 | 136 | |
104 | 137 | scanPara [] acc = (reverse acc, []) |
105 | scanPara (Line (Seq []):ls) acc = (reverse acc, ls) | |
138 | scanPara (Grp Line (Seq []):ls) acc = (reverse acc, ls) | |
106 | 139 | scanPara (l:ls) acc = scanPara ls (l:acc) |
107 | 140 | |
108 | 141 | scanBlankLines [] acc = (reverse acc, []) |
109 | scanBlankLines (l@(Line (Seq [])):ls) acc = scanBlankLines ls (l:acc) | |
142 | scanBlankLines (l@(Grp Line (Seq [])):ls) acc = scanBlankLines ls (l:acc) | |
110 | 143 | scanBlankLines (l:ls) acc = (reverse acc, (l:ls)) |
111 | 144 | |
112 | 145 | |
114 | 147 | |
115 | 148 | replace a b (Seq many) = |
116 | 149 | Seq $ map (replace a b) many |
117 | replace a b t@(Word txt) | |
150 | replace a b t@(Grp typ txt) | |
118 | 151 | | t == a = b |
119 | | otherwise = Word $ replace a b txt | |
120 | replace a b t@(Line txt) | |
121 | | t == a = b | |
122 | | otherwise = Line $ replace a b txt | |
123 | replace a b t@(Para txt) | |
124 | | t == a = b | |
125 | | otherwise = Para $ replace a b txt | |
152 | | otherwise = Grp typ $ replace a b txt | |
126 | 153 | replace a b t@(Char c) |
127 | 154 | | t == a = b |
128 | 155 | | otherwise = t |
156 | ||
157 | ||
158 | concatSeq t (Seq ts) = | |
159 | Seq (t:ts) | |
160 | ||
161 | ||
162 | filt pred (Seq []) = (Seq []) | |
163 | filt pred (Seq (t:ts)) = | |
164 | case pred t of | |
165 | True -> concatSeq (filt pred t) (filt pred (Seq ts)) | |
166 | False -> filt pred (Seq ts) | |
167 | filt pred (Grp typ txt) = | |
168 | Grp typ $ filt pred txt | |
169 | filt pred c@(Char _) = | |
170 | c |