-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh9.hs
More file actions
121 lines (97 loc) · 2.73 KB
/
Ch9.hs
File metadata and controls
121 lines (97 loc) · 2.73 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module Ch9 where
import Data.Char
eft :: (Ord a, Enum a) => a -> a -> [a]
eft a a'
| compare a a' == LT = [a] ++ eft (succ a) a'
| compare a a' == EQ = [a]
| otherwise = []
eftBool :: Bool -> Bool -> [Bool]
eftBool = eft
-- | compare b b' == LT = [b] ++ eftBool (not b) b'
-- | compare b b' == EQ = [b]
-- | otherwise = []
-- [True .. False] ==> []
-- [False .. True] ==> [False,True]
-- [False .. False] ==> [False]
-- [True .. True] ==> [True]
eftOrd :: Ordering -> Ordering -> [Ordering]
eftOrd = eft
-- eftOrd LT EQ = [LT, EQ]
-- eftOrd EQ GT = [EQ, GT]
-- eftOrd LT GT = [LT, EQ, GT]
-- eftOrd x y
-- | x == y = [x]
-- | otherwise = []
eftInt :: Int -> Int -> [Int]
eftInt = eft
-- eftInt i j
-- | i <= j = [i] ++ eftInt (i+1) j
-- | i == j = [i]
-- | otherwise = []
eftChar :: Char -> Char -> [Char]
eftChar = eft
-- eftChar c c'
-- | compare c c' == LT = [c] ++ eftChar (succ c) c'
-- | compare c c' == EQ = [c]
-- | otherwise = []
myWords :: String -> [String]
myWords s
| s /= "" = takeWhile (/= ' ') s : myWords rest
| otherwise = []
where
rest = dropWhile (== ' ') $ dropWhile (/= ' ') s
breakString :: Char -> String -> [String]
breakString c s
| s /= "" = takeWhile (/= c) s : breakString c rest
| otherwise = []
where
rest = dropWhile (== c) $ dropWhile (/= c) s
myLines :: String -> [String]
myLines = breakString '\n'
filterArticles :: String -> [String]
filterArticles s = filter (not . flip elem ["the", "a"]) words
where words = myWords s
myZip :: [a] -> [b] -> [(a,b)]
-- myZip (a:as) (b:bs) = (a,b) : myZip as bs
-- myZip [] _ = []
-- myZip _ [] = []
myZip = myZipWith (,)
myZipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
myZipWith f (a:as) (b:bs) = f a b : myZipWith f as bs
myZipWith _ [] _ = []
myZipWith _ _ [] = []
cap :: String -> String
cap s = toUpper (head s) : tail s
-- cap (s:ss) = toUpper s : ss
-- cap [] = []
capFirst :: String -> Char
capFirst = toUpper . head
-- capFirst s = toUpper $ head s
-- doesn't work right!!
cipher :: Int -> String -> String
cipher i s = map chr code
where
index = mod i 26
nums = map ord s
nums' = map (\x -> x-97) nums
code = map (\x -> (mod (x+index) 26) + 97) nums'
decipher :: Int -> String -> String
decipher i = cipher (-i)
rev :: [a] -> [a]
rev [] = []
rev (x:xs) = rev xs ++ [x]
squish :: [[a]] -> [a]
squish = squishMap id
-- squish [] = []
-- squish (x:xs) = x ++ squish xs
squishMap :: (a -> [b]) -> [a] -> [b]
squishMap f (x:xs) = f x ++ squishMap f xs
squishMap _ [] = []
maximBy :: (a -> a -> Ordering) -> [a] -> a
maximBy f (x:y:xs)
| f x y == GT = maximBy f (x:xs)
-- | f x y == EQ = maxim (x:xs)
| otherwise = maximBy f (y:xs)
maximBy f [x] = x
maxim :: Ord a => [a] -> a
maxim = maximBy compare