-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset4b.hs
123 lines (98 loc) · 3.62 KB
/
set4b.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
-- Exercise set 4b: folds
module Set4b where
import Mooc.Todo
------------------------------------------------------------------------------
-- Ex 1: countNothings with a fold. The function countNothings from
-- the course material can be implemented using foldr. Your task is to
-- define countHelper so that the following definition of countNothings
-- works.
--
-- Hint: You can start by trying to add a type signature for countHelper.
--
-- Challenge: look up the maybe function and use it in countHelper.
--
-- Examples:
-- countNothings [] ==> 0
-- countNothings [Just 1, Nothing, Just 3, Nothing] ==> 2
countNothings :: [Maybe a] -> Int
countNothings xs = foldr countHelper 0 xs
countHelper :: Maybe a -> Int -> Int
countHelper Nothing x = x + 1
countHelper _ x = x
------------------------------------------------------------------------------
-- Ex 2: myMaximum with a fold. Just like in the previous exercise,
-- define maxHelper so that the given definition of myMaximum works.
--
-- Examples:
-- myMaximum [] ==> 0
-- myMaximum [1,3,2] ==> 3
myMaximum :: [Int] -> Int
myMaximum [] = 0
myMaximum (x:xs) = foldr maxHelper x xs
maxHelper :: Int -> Int -> Int
maxHelper a b = max a b
------------------------------------------------------------------------------
-- Ex 3: compute the sum and length of a list with a fold. Define
-- slHelper and slStart so that the given definition of sumAndLength
-- works. This could be used to compute the average of a list.
--
-- Start by giving slStart and slHelper types.
--
-- Examples:
-- sumAndLength [] ==> (0.0,0)
-- sumAndLength [1.0,2.0,4.0] ==> (7.0,3)
sumAndLength :: [Double] -> (Double,Int)
sumAndLength xs = foldr slHelper slStart xs
slStart = (0.0, 0)
slHelper curr (sum, length) = (sum + curr, length + 1)
------------------------------------------------------------------------------
-- Ex 4: implement concat with a fold. Define concatHelper and
-- concatStart so that the given definition of myConcat joins inner
-- lists of a list.
--
-- Examples:
-- myConcat [[]] ==> []
-- myConcat [[1,2,3],[4,5],[6]] ==> [1,2,3,4,5,6]
myConcat :: [[a]] -> [a]
myConcat xs = foldr concatHelper concatStart xs
concatStart = []
concatHelper curr acc = curr ++ acc
------------------------------------------------------------------------------
-- Ex 5: get all occurrences of the largest number in a list with a
-- fold. Implement largestHelper so that the given definition of largest works.
--
-- Examples:
-- largest [] ==> []
-- largest [1,3,2] ==> [3]
-- largest [1,3,2,3] ==> [3,3]
largest :: [Int] -> [Int]
largest [] = []
largest xs = foldr largestHelper [] xs
largestHelper curr [] = [curr]
largestHelper curr tot@(x:xs) = if x == curr then [curr] ++ tot else if curr > x then [curr] else tot
------------------------------------------------------------------------------
-- Ex 6: get the first element of a list with a fold. Define
-- headHelper so that the given definition of myHead works.
--
-- Start by giving headHelper a type.
--
-- Examples:
-- myHead [] ==> Nothing
-- myHead [1,2,3] ==> Just 1
myHead :: [a] -> Maybe a
myHead [] = Nothing
myHead xs = foldr headHelper Nothing xs
headHelper curr _ = Just curr
------------------------------------------------------------------------------
-- Ex 7: get the last element of a list with a fold. Define lasthelper
-- so that the given definition of myLast works.
--
-- Start by giving lastHelper a type.
--
-- Examples:
-- myLast [] ==> Nothing
-- myLast [1,2,3] ==> Just 3
myLast :: [a] -> Maybe a
myLast xs = foldr headHelper Nothing xs
lastHelper curr Nothing = Just curr
lastHelper curr acc = acc