-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch12.hs
153 lines (118 loc) · 3.75 KB
/
ch12.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module Main where
main :: IO ()
main = return ()
notThe :: String -> Maybe String
notThe "the" = Nothing
notThe str = Just str
replaceThe :: String -> String
replaceThe str = unwords $ map f $ words str
where f "the" = "a"
f x = x
isVowel :: Char -> Bool
isVowel c = elem c "aeiouAEIOU"
startsWithVowel :: String -> Bool
startsWithVowel [] = False
startsWithVowel (x:xs)
| isVowel x = True
| otherwise = False
countTheBeforeVowel :: String -> Integer
countTheBeforeVowel str =
let words = wordsAfterThe str
in fromIntegral $ length $ filter startsWithVowel words
wordsAfterThe :: String -> [String]
wordsAfterThe str = go $ words str
where go [] = []
go [x] = []
go ("the":x:xs) = x : go xs
go (x:xs) = go xs
countVowels :: String -> Integer
countVowels str = fromIntegral $ length $ filter startsWithVowel $ words str
newtype Word' = Word' String deriving (Eq, Show)
vowels = "aeiou"
mkWord :: String -> Maybe Word'
mkWord str =
let vowelCount = length $ filter isVowel str
otherCount = (length str) - vowelCount
in if vowelCount > otherCount
then Nothing
else Just (Word' str)
data Nat = Zero | Succ Nat deriving (Eq, Show)
natToInteger :: Nat -> Integer
natToInteger Zero = 0
natToInteger (Succ n) = 1 + (natToInteger n)
integerToNat :: Integer -> Maybe Nat
integerToNat x
| x < 0 = Nothing
| otherwise = Just $ go x
where go 0 = Zero
go n = Succ (go (n - 1))
isJust :: Maybe a -> Bool
isJust (Just _) = True
isJust _ = False
isNothing :: Maybe a -> Bool
isNothing Nothing = True
isNothing _ = False
mayybee :: b -> (a -> b) -> Maybe a -> b
mayybee x f Nothing = x
mayybee _ f (Just x) = f x
fromMaybe :: a -> Maybe a -> a
fromMaybe x Nothing = x
fromMaybe _ (Just x) = x
listToMaybe :: [a] -> Maybe a
listToMaybe [] = Nothing
listToMaybe (x:xs) = Just x
maybeToList :: Maybe a -> [a]
maybeToList Nothing = []
maybeToList (Just x) = [x]
catMaybes :: [Maybe a] -> [a]
catMaybes [] = []
catMaybes (Nothing:xs) = catMaybes xs
catMaybes ((Just x):xs) = x : catMaybes xs
flipMaybe :: [Maybe a] -> Maybe [a]
flipMaybe maybes
| hasNothing maybes = Nothing
| otherwise = Just $ map (\(Just x) -> x) maybes
hasNothing xs = foldr ((||) . isNothing) False xs
lefts' :: [Either a b] -> [a]
lefts' = foldr appendLeft []
where appendLeft (Right _) acc = acc
appendLeft (Left x) acc = x: acc
rights' :: [Either a b] -> [b]
rights' = foldr appendRight []
where appendRight (Left _) acc = acc
appendRight (Right x) acc = x: acc
partitionEithers' :: [Either a b] -> ([a], [b])
partitionEithers' = foldr partition' ([], [])
where partition' (Left x) (lefts, rights) = (x : lefts, rights)
partition' (Right x) (lefts, rights) = (lefts, x : rights)
eitherMaybe' :: (b -> c) -> Either a b -> Maybe c
eitherMaybe' _ (Left _) = Nothing
eitherMaybe' f (Right x) = Just $ f x
either' :: (a -> c) -> (b -> c) -> Either a b -> c
either' f g (Left x) = f x
either' f g (Right x) = g x
eitherMaybe'' :: (b -> c) -> Either a b -> Maybe c
eitherMaybe'' f m = either' (\_ -> Nothing) (\x -> Just $ f x) m
myIterate :: (a -> a) -> a -> [a]
myIterate f x = x : myIterate f (f x)
myUnfoldr :: (b -> Maybe (a, b)) -> b -> [a]
myUnfoldr f x =
case f x of
Just (x1, x2) -> x1 : myUnfoldr f x2
Nothing -> []
betterIterate :: (a -> a) -> a -> [a]
betterIterate f x = myUnfoldr (\y -> Just (y, f y)) x
data BinaryTree a =
Leaf
| Node (BinaryTree a) a (BinaryTree a)
deriving (Eq, Ord, Show)
unfold :: (a -> Maybe (a,b,a)) -> a -> BinaryTree b
unfold f x =
case f x of
Just (x1, x2, x3) -> Node (unfold f x1) x2 (unfold f x3)
Nothing -> Leaf
treeBuild :: Integer -> BinaryTree Integer
treeBuild n = unfold f 0
where f x
| x == n = Nothing
| otherwise = Just (x+1, x, x+1)