-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter11aspatterns.hs
39 lines (29 loc) · 1.2 KB
/
chapter11aspatterns.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Data.Char
isSubsequenceOf :: (Eq a) => [a] -> [a] -> Bool
isSubsequenceOf [] _ = True
isSubsequenceOf _ [] = False
isSubsequenceOf ax@(x:xs) (y:ys)
| x == y = isSubsequenceOf xs ys
| otherwise = isSubsequenceOf ax ys
capitalizeWord :: String -> String
capitalizeWord "" = ""
capitalizeWord (c:rem) = (toUpper c) : rem
capitalizeWords :: String -> [(String, String)]
capitalizeWords s = [(w, capitalizeWord w) | w <- words s]
-- using asPattern:
-- capitalizeWords xs = map f $ words xs
-- where f as@(s:st) = (as, toUpper s : st)
doCapitalizeParagraph :: String -> [String] -> [String]
doCapitalizeParagraph _ [] = []
doCapitalizeParagraph pword (w:ws)
| (last pword) == '.' = capitalizeWord w : doCapitalizeParagraph w ws
| otherwise = w : doCapitalizeParagraph w ws
capitalizeParagraph :: String -> String
capitalizeParagraph p
| paragraphWords == [] = p
| tail paragraphWords == [] = p
| otherwise = unwords ([capitalizeWord (head paragraphWords)] ++ doCapitalizeParagraph (head paragraphWords) (tail paragraphWords))
where paragraphWords = words p
-- alternative:
-- capitalizeParagraph :: String -> String
-- capitalizeParagraph xs = concatMap capitalizeWord $ groupBy ((==) `on` (=='.')) xs