-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch11ex.hs
47 lines (38 loc) · 1.35 KB
/
ch11ex.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
40
41
42
43
44
45
46
47
module Ch11Ex where
import Data.Char
ord_a = ord 'a'
ord_z = ord 'z'
vigenere :: String -> String -> String
vigenere shiftText text =
let infShiftText = map shiftNum $ (concat . repeat) $ shiftText
in go infShiftText text
where go _ [] = []
go xs (' ':ys) = ' ' : go xs ys
go (x:xs) (y:ys) = (shiftChar x y) : go xs ys
shiftNum :: Char -> Int
shiftNum c = (ord c) - ord_a
shiftChar :: Int -> Char -> Char
shiftChar s c = chr $ normalize $ s + ord c
where normalize x
| x < ord_a = x + ord_z - ord_a + 1
| x > ord_z = x - ord_z + ord_a - 1
| otherwise = x
isSubsequenceOf :: (Eq a) => [a] -> [a] -> Bool
isSubsequenceOf [] _ = True
isSubsequenceOf _ [] = False
isSubsequenceOf xxs@(x:xs) (y:ys)
| x == y = isSubsequenceOf xs ys
| otherwise = isSubsequenceOf xxs ys
capitalizeWords :: String -> [(String, String)]
capitalizeWords = map go . words
where go [] = ("", "")
go xxs@(x:xs) = (xxs, (toUpper x) : xs)
capitalizeWord :: String -> String
capitalizeWord [] = []
capitalizeWord (x:xs) = toUpper x : xs
capitalizeParagraph :: String -> String
capitalizeParagraph [] = []
capitalizeParagraph xs = capitalizeWord paragraph ++ capitalizeWord rest
where (s,r) = break (== '.') xs
paragraph = s ++ takeWhile (\c -> c == '.' || c == ' ') r
rest = dropWhile (\c -> c == '.' || c == ' ') r