-
Notifications
You must be signed in to change notification settings - Fork 0
/
folding.hs
181 lines (138 loc) · 4.83 KB
/
folding.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
module FoldingLists where
import Data.Time
import Data.List
left = foldl (+) 0 (1 : 2 : 3 : [])
-- ((0 + 1) + 2) + 3
right = foldr (+) 0 (1 : 2 : 3 : [])
-- (1 + (2 + (3 + 0)))
-- foldl (flip (*)) 1 [1..3]
-- (3 * (2 * (1 * 1)))
a = foldr (++) [] ["woot", "WOOT", "woot"]
b = foldr max [] ["fear", "is", "the", "little", "death"]
c = foldr (&&) True [False, True]
d = foldr (||) True [False, True]
e = foldl (flip $ (++) . show) "" [1..5]
f = foldr (flip const) 'a' [1..5]
g = foldr (flip const) 0 "tacos"
h = foldl const 0 "burritos"
i = foldl const 'z' [1..5]
data DatabaseItem =
DbString String
| DbNumber Integer
| DbDate UTCTime
deriving (Eq, Ord, Show)
theDatabase :: [DatabaseItem]
theDatabase =
[ DbDate (UTCTime
(fromGregorian 1911 5 1)
(secondsToDiffTime 34123))
, DbString "Hello World"
, DbDate (UTCTime
(fromGregorian 1921 5 1)
(secondsToDiffTime 34123))
]
filterDbDate :: [DatabaseItem] -> [UTCTime]
filterDbDate = foldr f []
where f (DbDate time) list = [time] ++ list
f _ list = list
filterDbNumber :: [DatabaseItem] -> [Integer]
filterDbNumber = foldr f []
where f (DbNumber i) list = [i] ++ list
f _ list = list
mostRecent :: [DatabaseItem] -> UTCTime
mostRecent xs = foldr max (head dates) (tail dates)
where dates = filterDbDate xs
sumDb :: [DatabaseItem] -> Integer
sumDb = foldr (+) 0 . filterDbNumber
avgDb :: [DatabaseItem] -> Double
avgDb xs = dbSum / dbLength
where dbSum = fromIntegral $ sumDb xs
dbLength = fromIntegral $ length $ filterDbNumber xs
myFibs :: Integer -> Integer
myFibs 0 = 1
myFibs 1 = 1
myFibs n = (myFibs (n - 1)) + (myFibs (n - 2))
-- 1 : [1 + 1, 1 + 2, 2 + 3]
fibs = 1 : scanl (+) 1 fibs
-- take 3 fibs
-- 1 : scanl (+) 1 fibs
-- 1 : scanl (+) 1 (1 : scanl (+) 1 fibs)
-- 1 : scanl (+) 1 (1 : scanl (+) 1 (1 : scanl (+) 1 fibs))
-- v-------vv-------v
-- [1, (1 + 1), (1 + 2), (2 + 3), (3 + 5)]
-- \----/ \----/
--
-- 1 : scanl (+) 1 fibs
-- 1 : scanl (+) 1 (1 : scanl (+) 1 fibs)
-- 1 : 1 : scanl (+) (+ 1 1) (scanl (+) 1 fibs)
-- 1 : 1 : scanl (+) 2 (scanl (+) 1 (1 : scanl (+) 1 fibs))
-- 1 : 1 : scanl (+) 2 (1 : scanl (+) (+ 1 1) (scanl (+) 1 fibs))
-- 1 : 1 : 2 scanl (+) (+ 2 1) (scanl (+) 2 (scanl (+) 1 fibs))
-- 1 : 1 : 2 scanl (+) 3 (scanl (+) 2 (scanl (+) 1 fibs))
-- 1 : 1 : 2 scanl (+) 3 (scanl (+) 2 (1 : scanl (+) (+ 1 1) ()))
-- 1 : 1 : 2 : scanl (+) (+ 2 1) (scanl (+) (+ 1 1) fibs)
-- 1 : 1 : 2 : scanl (+) 3 (2 : scanl (+) (+ 2 1) (scanl (+) 1 fibs))
-- 1 : 1 : 2 : 3 : scanl (+) (+ 3 2) (scanl (+) 3 (scanl (+) 1 fibs))
-- 1 : 1 : 2 : 3 : scanl (+) 5 (3 : scanl (+) (+ 3 ))
-- fibs = [1 : 1 : 2 : 3]
myFactorial :: Integer -> Integer
myFactorial 0 = 1
myFactorial n = n * (myFactorial (n - 1))
-- assume we can generate factorial of n
-- how do we generate factorial of n+1?
-- fact 0 = 1
-- fact n = n * (fact (n - 1))
factorial = scanl (*) 1 [2..]
-- v vvvvv vvvvvvvvv
-- [1, (1 * 2), (1 * 2 * 3), (1 * 2 * 3 * 4)]
--
-- (+) 0 [1..10]
-- (0 + (1 + (2 + (3 + ))))
myFoldr :: (a -> b -> b) -> b -> [a] -> b
myFoldr _ b [] = b
myFoldr f b (x:xs) = f x $ myFoldr f b xs
myFoldl :: (b -> a -> b) -> b -> [a] -> b
myFoldl _ b [] = b
myFoldl f b (x:xs) = myFoldl f (f b x) xs
stops = "pbtdkg"
vowels = "aeiou"
combos = [[s,v,s'] | s <- stops, v <- vowels, s' <- stops, s == 'p']
nouns = ["house", "dog", "tree", "cat"]
verbs = ["hug", "bite", "run", "grow", "whistle", "sleep"]
combos' = [(n, v, n') | n <- nouns, v <- verbs, n' <- nouns]
avgLengthOfWord x = fromIntegral (sum (map length (words x))) / (fromIntegral (length (words x)))
myOr :: [Bool] -> Bool
myOr = foldr (||) False
myAny :: (a -> Bool) -> [a] -> Bool
myAny f = foldr ((||) . f) False
myElem :: Eq a => a -> [a] -> Bool
myElem = myAny . (==)
myReverse :: [a] -> [a]
myReverse = foldl (flip (:)) []
myMap :: (a -> b) -> [a] -> [b]
myMap f = foldr ((:) . f) []
myFilter :: (a -> Bool) -> [a] -> [a]
myFilter f = foldr z []
where z a b = if f a then [a] ++ b
else b
squish :: [[a]] -> [a]
squish = foldr (++) []
squishMap :: (a -> [b]) -> [a] -> [b]
squishMap f = foldr ((++) . f) []
squishAgain :: [[a]] -> [a]
squishAgain = squishMap id
-- need to use foldl instead of foldr to ensure L->R ordering
-- myMaximumBy (\_ _ -> GT) [1..10] should == 1
-- myMaximumBy (\_ _ -> LT) [1..10] should == 10
myMaximumBy :: (a -> a -> Ordering) -> [a] -> a
myMaximumBy _ (x:[]) = x
myMaximumBy f (x:xs) = foldl takeGT x xs
where takeGT a b = case (f a b) of
GT -> a
otherwise -> b
myMinimumBy :: (a -> a -> Ordering) -> [a] -> a
myMinimumBy _ (x:[]) = x
myMinimumBy f (x:xs) = foldl takeLT x xs
where takeLT a b = case (f a b) of
LT -> a
otherwise -> b