git @ Cat's Eye Technologies The-Dipple / b1a89d6
Grp Type and filt function. Chris Pressey 10 years ago
1 changed file(s) with 59 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
11
22 import System.Environment
33
4 data Type = Word | Line | Para
5 deriving (Show, Ord, Eq)
6
47 data Text = Char String
5 | Word Text
6 | Line Text
7 | Para Text
8 | Grp Type Text
89 | Seq [Text]
910 deriving (Show, Ord, Eq)
1011
1516 whitespace _ = False
1617
1718
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
1836 main = do
1937 [fileName] <- getArgs
2038 run fileName
2139
2240 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
2356 a <- readToChars fileName
2457
2558 let b = groupCharsToLines a
5992 let
6093 (line, rest) = scanLine cs []
6194 in
62 ((Line $ Seq $ line):(g rest))
95 ((Grp Line $ Seq $ line):(g rest))
6396
6497 scanLine [] acc = (reverse acc, [])
6598 scanLine (Char "\n":cs) acc = (reverse acc, cs)
75108 (word, rest) = scanWord cs []
76109 (spc, rest') = scanSpaces rest []
77110 in
78 ((Word $ Seq $ word):(Word $ Seq $ spc):(g rest'))
111 ((Grp Word $ Seq $ word):(Grp Word $ Seq $ spc):(g rest'))
79112
80113 scanWord [] acc = (reverse acc, [])
81114 scanWord (c:cs) acc =
87120
88121
89122 groupLinesToWords (Seq many) =
90 Seq $ map (\(Line x) -> Line $ groupCharsToWords x) many
123 Seq $ map (\(Grp Line x) -> Grp Line $ groupCharsToWords x) many
91124
92125
93126 groupLinesToParas (Seq many) =
99132 (lines, rest) = scanPara cs []
100133 (spc, rest') = scanBlankLines rest []
101134 in
102 ((Para $ Seq $ lines):(g rest'))
135 ((Grp Para $ Seq $ lines):(g rest'))
103136
104137 scanPara [] acc = (reverse acc, [])
105 scanPara (Line (Seq []):ls) acc = (reverse acc, ls)
138 scanPara (Grp Line (Seq []):ls) acc = (reverse acc, ls)
106139 scanPara (l:ls) acc = scanPara ls (l:acc)
107140
108141 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)
110143 scanBlankLines (l:ls) acc = (reverse acc, (l:ls))
111144
112145
114147
115148 replace a b (Seq many) =
116149 Seq $ map (replace a b) many
117 replace a b t@(Word txt)
150 replace a b t@(Grp typ txt)
118151 | 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
126153 replace a b t@(Char c)
127154 | t == a = b
128155 | 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