-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter7.hs
More file actions
62 lines (47 loc) · 1.5 KB
/
Chapter7.hs
File metadata and controls
62 lines (47 loc) · 1.5 KB
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
-- Exercise 1
reexpr :: (a -> a) -> (a -> Bool) -> [a] -> [a]
reexpr f p xs = map f (filter (p) xs)
-- Exercise 2
allo :: (a -> Bool) -> [a] -> Bool
allo p = and . map (p)
anyo :: (a -> Bool) -> [a] -> Bool
anyo p = or . map (p)
takeWhileo :: (a -> Bool) -> [a] -> [a]
takeWhileo p [] = []
takeWhileo p (x:xs) | p x = x : takeWhileo (p) xs
| otherwise = []
dropWhileo :: (a -> Bool) -> [a] -> [a]
dropWhileo p [] = []
dropWhileo p (x:xs) | p x = dropWhile (p) xs
| otherwise = x:xs
-- Exercise 3
mapfr :: (a -> b) -> [a] -> [b]
mapfr f [] = []
mapfr f xs = foldr (\x y -> f x : y) [] xs
filterfr' :: (a -> Bool) -> a -> [a]
filterfr' p x | p x = [x]
| otherwise = []
filterfr :: (a -> Bool) -> [a] -> [a]
filterfr p = foldr (\x y -> (filterfr' p x) ++ y ) []
-- Exercise 4
dec2int :: [Int] -> Int
dec2int = foldl (\x y -> x*10 + y) 0
-- Exercise 5
curryo :: ((a,b) -> c) -> a -> b -> c
curryo f x y = f (x, y)
uncurryo :: (a -> b -> c) -> (a,b) -> c
uncurryo f = \(x,y) -> f x y
-- Exercise 6
unfold :: (t -> Bool) -> (t -> a) -> (t -> t) -> t -> [a]
unfold p h t x | p x = []
| otherwise = h x : unfold p h t (t x)
chop8 :: [Int] -> [[Int]]
chop8 = unfold null (take 8) (drop 8)
mapo :: (a -> b) -> [a] -> [b]
mapo f = unfold null (f . head) (drop 1)
iterateo :: (a -> a) -> a -> [a]
iterateo f x = unfold (const True) (f . head ) (id) [x]
-- Exercise 9
altMap :: (a -> b) -> (a -> b) -> [a] -> [b]
altMap f1 f2 [] = []
altMap f1 f2 (x:xs) = f1 x : altMap f2 f1 xs