-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfold.hs
64 lines (39 loc) · 1.18 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
-- sum' :: [Int] -> Int
-- sum' [] = 0
-- sum' (x:xs) = x + sum' xs
-- concat' :: [[a]] -> [a]
-- concat' [] = []
-- concat' (xs:xss) = xs ++ concat' xss
sum' :: [Int] -> Int
sum' = sum
foldr' :: (a -> b -> b) -> b -> [a] -> b
foldr' _ z [] = z
foldr' f z (x:xs) = f x (foldr' f z xs)
any' :: [Bool] -> Bool
-- any' = foldr (||) False
any' = or
all' :: [Bool] -> Bool
-- all' = foldr (&&) True
all' = and
product' :: [Int] -> Int
-- product' = foldr (*) 1
product' = product
-- elem' :: (a -> Bool) -> [a] -> Bool
-- elem' el [] = False
-- elem' el (x:xs) = el x || elem' el xs
elem' el = any' . map (== el)
mapAsFold :: (a -> b) -> [a] -> [b]
mapAsFold f = map (\ x -> f x)
mapAsLeftFold :: (a -> b) -> [a] -> [b]
mapAsLeftFold f = foldl (\xs x -> f x : xs) []
-- mapAsFold (*2) [1..3]
-- mapAsLeftFold (*2) [1..3]
filterAsFold :: (a -> Bool) -> [a] -> [a]
filterAsFold p = foldr (\x xs -> if p x then x:xs else xs) []
-- foldl :: (b -> a -> b) -> b -> [a] -> b
-- foldl _ z [] = z
-- foldl f z (x:xs) = foldl f (f z x) xs
foldReverse :: [a] -> [a]
foldReverse = foldl (flip (:)) []
-- main = print (foldReverse "sdrawkcab")
-- maxInt = foldr1 (\x y -> if x > y then x else y)