-
Notifications
You must be signed in to change notification settings - Fork 0
/
fold.hs
156 lines (113 loc) · 3.4 KB
/
fold.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
import Data.Time
--C:\Users\achen\Desktop\Haskell\fold.hs
--- :set prompt "\x03BB> "
data DatabaseItem = DbString String
| DbNumber Integer
| DbDate UTCTime
deriving (Eq, Ord, Show)
theDatabase :: [DatabaseItem]
theDatabase =
[ DbDate (UTCTime
(fromGregorian 1911 5 1)
(secondsToDiffTime 34250))
, DbNumber 9001
, DbString "Hello, world!"
, DbDate (UTCTime
(fromGregorian 1921 5 1)
(secondsToDiffTime 34123))
]
--filterDbDate :: [DatabaseItem] -> [UTCTime]
--filterDbDate x = head x
reverse' :: [a] -> [a]
reverse' x = foldl (flip (:)) [] x
length':: (Foldable t, Num b) => t b -> b
length' x = foldl (\_ y -> y + 1) 0 x
--onlyThree :: Integral a => [a] -> [a]
--onlyThree z= filter (\x -> mod x 3 == 0) z
fibs :: [Integer]
fibs = 1 : scanl (+) 1 fibs
fibsTwenty :: [Integer]
fibsTwenty = take 20 $ 1 : scanl (+) 1 fibs
fibsLessHunderd :: [Integer]
fibsLessHunderd = filter (< 100) $ take 300 $ 1 : scanl (+) 1 fibs
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n-1)
--factorial' :: Int -> Integer
--factorial' n = factorials !! n
-- where
-- factorials :: [Integer]
-- factorials = scanl (*) 1 [1..]
factorials :: [Integer]
factorials = scanl (*) 1 [2..]
stops :: [Char]
stops = "pbtdkg"
vowels :: [Char]
vowels = "aeiou"
seekritFunc :: String -> Float
seekritFunc x = fromIntegral (sum(map length (words x))) / fromIntegral (length (words x))
-- if else
myOr :: [Bool] -> Bool
myOr [] = False
myOr (x:xs) =
if x == True
then True
else myOr xs
--recursive
myOr' :: [Bool] -> Bool
myOr' [] = False
myOr' (x:xs) = x || myOr' xs
--fold without point free notation
myOr'' :: [Bool] -> Bool
myOr'' = foldr (\a b -> if a == True then True else b) False
--fold with point free notation
myOr''' :: [Bool] -> Bool
myOr''' = foldr (||) False
--any function with if else
myAny :: (a-> Bool) -> [a] -> Bool
myAny _ [] = False
myAny f (x:xs) = if f x == True then True else myAny f xs
--any function direct recusrion
myAny' :: (a-> Bool) -> [a] -> Bool
myAny' _ [] = False
myAny' f (x:xs)= f x == True || myAny' f xs
--any function foldr
myAny'' :: (a-> Bool) -> [a] -> Bool
myAny'' f = foldr (\a b -> f a || b) False
--any function point free
-- elem with if else statement
myElem :: Eq a => a -> [a] -> Bool
myElem _ [] = False
myElem y (x:xs) = if y == x then True else myElem y xs
--elem with direct recursion
myElem' :: Eq a => a -> [a] -> Bool
myElem' _ [] = False
myElem' y (x:xs) = y == x || myElem' y xs
--elem with foldr
--myElem'' :: Eq a => a -> [a] -> Bool
--myElem'' = foldr (\a b -> )
--elem with any
myElemAny :: Eq a => a -> [a] -> Bool
myElemAny a = any (== a)
--elem with any
myElemAny' :: Eq a => a -> [a] -> Bool
myElemAny' a xs= any (== a) xs
--elem with fold
myElemFold :: Eq a => a -> [a] -> Bool
myElemFold y = foldr (\x b -> x == y || b) False
--myReverse
myReverse :: [a] -> [a]
myReverse [] = []
myReverse xs = last xs : myReverse (init xs)
myReverse' :: [a] -> [a]
myReverse' = foldl (flip (:)) []
myMap :: (a -> b) -> [a] -> [b]
myMap f = foldr ((:).f) []
myFilter :: (a -> Bool) -> [a] -> [a]
myFilter f = foldr (\x xs -> if f x then x:xs else xs) []
squish :: [[a]] -> [a]
squish = foldr (++) []
squishMap :: (a-> [b])-> [a]-> [b]
squishMap f = foldr ((++) . f) []
squishAgain :: [[a]] -> [a]
squishAgain = squishMap id