-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapterTen.hs
102 lines (73 loc) · 2.7 KB
/
chapterTen.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
module ChapterTen where
import Data.Time
data DatabaseItem = DbString String
| DbNumber Integer
| DbDate UTCTime
deriving (Eq, Ord, Show)
{--foldright = foldr (\x y -> concat ["(",x,"+",y,")"]) "0" (map show [1..5])
foldleft = foldl (\x y -> concat ["(",x,"+",y,")"]) "0" (map show [1..5])--}
theDatabase :: [DatabaseItem]
theDatabase =
[ DbDate (UTCTime
(fromGregorian 1911 5 1)
(secondsToDiffTime 34123)) , DbNumber 9001
, DbString "Hello, world!"
, DbDate (UTCTime
(fromGregorian 1921 5 1)
(secondsToDiffTime 34123))
]
filterDbDate :: [DatabaseItem] -> [UTCTime]
filterDbDate [] = []
filterDbDate ((DbDate time):xs) = time : filterDbDate xs
filterDbNumber :: [DatabaseItem] -> [Integer]
filterDbNumber [] = []
filterDbNumber ((DbNumber num):xs) = num : filterDbNumber xs
mostRecent :: [DatabaseItem] -> UTCTime
mostRecent items = foldr max time (filterDbDate items)
where time = UTCTime
(fromGregorian 1000 5 1)
(secondsToDiffTime 34123)
sumDb :: [DatabaseItem] -> Integer
sumDb = sum . filterDbNumber
avgDb :: [DatabaseItem] -> Double
avgDb items = let total = sumDb items
size = length $ filterDbNumber items
in fromIntegral total / fromIntegral size
-- nth fibonacci
fibs = 1 : scanl (+) 1 fibs
fibsN x = fibs !! x
firstNFibs n = take n fibs -- take 20
firstFibsLessThanN n = filter (< n) (take 200 fibs)
-- factorial as scanl
factorial = scanl (*) 1 [1..]
{--
data MyType = MyInt Int | MyChar Char
foo :: MyType -> MyType
foo (MyInt i) = MyInt (i + 1) -- Or whatever
foo (MyChar c) = case c of {'a' -> MyChar 'A'; k -> MyChar k}
--}
-- 10.10
threeTuples = let stops = "pbtdkg"
vowels = "aeiou"
in [ ( s, v, s ) | s <- stops, v <- vowels ]
threeTuplesStartsWith char = filter (\(x, _, _) -> x == char) threeTuples
seekritFunc x = sum $ map length $ words x -- / length (words x)
-- point free version
myOr :: [Bool] -> Bool
myOr = foldr (||) False
myAny :: (a -> Bool) -> [a] -> Bool
myAny f = foldr (\x y -> f x || y) False
myElem :: Eq a => a -> [a] -> Bool
myElem x = myAny (==x)
myReverse :: [a] -> [a]
myReverse = reverse . foldr (:) []
myMap :: (a -> b) -> [a] -> [b]
myMap f = foldr ((:) . f) []
myFilter :: (a -> Bool) -> [a] -> [a]
myFilter f = foldr (\a b -> if f a then a : b else b) []
squish :: [[a]] -> [a]
squish = foldr (++) []
squishMap :: (a -> [b]) -> [a] -> [b]
squishMap f xs = foldr (++) [] $ map f xs
myMaximumBy :: (a -> a -> Ordering) -> [a] -> a
myMaximumBy = undefined--foldr (\a b -> )