blob: 412de32ce5dce1128be63a6dfd03ad35fc460f15 (
plain)
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
|
module LibraryFunctions where
-- 1
sum :: (Foldable t, Num a) => t a -> a
sum = foldr (+) 0
-- 2
product :: (Foldable t, Num a) => t a -> a
product = foldr (*) 0
-- 3
elem :: (Foldable t, Eq a) => a -> t a -> Bool
elem needle = foldr (\x b -> b || (needle == x)) False
-- 4
minimum :: (Foldable t, Ord a) => t a -> Maybe a
minimum = foldr f Nothing
where f x Nothing = Just x
f x (Just y)
| x > y = Just x
| otherwise = Just y
-- 5
maximum :: (Foldable t, Ord a) => t a -> Maybe a
maximum = foldr f Nothing
where f x Nothing = Just x
f x (Just y)
| x < y = Just x
| otherwise = Just y
-- 6
null :: (Foldable t) => t a -> Bool
null f = LibraryFunctions.length f == 0
-- 7
length :: (Foldable t) => t a -> Int
length = foldr (\_ l -> l + 1) 0
-- 8
toList :: (Foldable t) => t a -> [a]
toList = foldr (:) []
-- 9
-- | Combine the elements of a structure using a monoid.
--
fold :: (Foldable t, Monoid m) => t m -> m
fold = LibraryFunctions.foldMap id
-- 10
foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m
foldMap f t = foldr (\x m -> mappend (f x) m) mempty t
|