-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture-05.hs
173 lines (148 loc) · 4.28 KB
/
lecture-05.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{-|
Module : Lecture5Exercises
Description : Solutions to Lecture 5 exercises
Maintainer : Dinko Osrecki
-}
module Lecture5Exercises where
-- EXERCISE 01 ----------------------------------------------------------------
{-
1.1
- Define a recursive function to compute the product of a list of elements.
-}
product' :: (Num a) => [a] -> a
product' [] = 1
product' (x:xs) = x * product' xs
-- tail recursive
product'' :: (Num a) => [a] -> a
product'' xs = f 1 xs
where
f acc (y:ys) = f (acc * y) ys
f acc [] = acc
-- usign foldr
product''' :: (Num a) => [a] -> a
product''' xs = foldr (*) 1 xs
{-
1.2
- Define a recursive function which takes a list of lists and returns
a list of their heads.
-}
headsOf :: [[a]] -> [a]
headsOf [] = []
headsOf ((x:_):xss) = x : headsOf xss
headsOf ([]:xss) = headsOf xss
-- EXERCISE 02 ----------------------------------------------------------------
{-
2.1
- Define a recursive function 'modMult n m xs' which multiplies each
element of a list 'xs' with 'n' modulo 'm'.
-}
modMult :: (Integral a, Num b) => a -> a -> [b] -> [b]
modMult _ _ [] = []
modMult n m (x:xs) = x' : modMult n m xs
where
x' = x * fromIntegral (n `mod` m)
{-
2.2
- Define a function which adds the value of the preceding element to
each element of the list. The first element gets no value added.
-}
addPredecessor :: Num a => [a] -> [a]
addPredecessor xs = f (0:xs) []
where
f (x:y:ys) acc = f (y:ys) (acc ++ [x+y])
f _ acc = acc
-- EXERCISE 03 ----------------------------------------------------------------
{-
3.1
- Define a function which, given a list of triplets (x,y,z), filters all
triplets for which x==y==z.
-}
equalTriplets :: (Eq a) => [(a, a, a)] -> [(a, a, a)]
equalTriplets [] = []
equalTriplets ((x,y,z):xs)
| x == y && x == z = (x,y,z) : equalTriplets xs
| otherwise = equalTriplets xs
-- using list comprehension
equalTriplets' :: (Eq a) => [(a, a, a)] -> [(a, a, a)]
equalTriplets' xs = [(x,y,z) | (x,y,z) <- xs, x == y, x ==z]
-- using filter
equalTriplets'' :: (Eq a) => [(a, a, a)] -> [(a, a, a)]
equalTriplets'' xs = filter allEq xs
where
allEq (x,y,z) = x == y && x == z
{-
3.2
- Define your own version of the replicate function.
-}
replicate' :: Int -> a -> [a]
replicate' 0 _ = []
replicate' n x = x : replicate' (n - 1) x
-- tail recursive
replicate'' :: Int -> a -> [a]
replicate'' n x = f n []
where
f 0 acc = acc
f m acc = f (m - 1) (x:acc)
-- EXERCISE 04 ----------------------------------------------------------------
{-
4
- Extend 'take' so that, if n > length xs, the last element of the list
gets repeated.
-}
take'' :: Int -> [a] -> [a]
take'' n xs
| n > length xs = take n $ xs ++ repeat (last xs)
| otherwise = take n xs
{-
4.1 a
- Define your own recursive version of the drop function.
-}
drop' :: Int -> [a] -> [a]
drop' n xs | n <= 0 = xs
drop' _ [] = []
drop' n (_:xs) = drop' (n - 1) xs
{-
4.1 b
- Define drop'' (a wrapper function) so that for n < 0 the function drops
the elements from the end of the list.
-}
drop'' :: Int -> [a] -> [a]
drop'' n xs
| n < 0 = reverse $ drop' (abs n) (reverse xs)
| otherwise = drop' n xs
{-
4.2
- Define a recursive function 'takeFromTo n1 n2 xs'.
-}
takeFromTo :: Int -> Int -> [a] -> [a]
takeFromTo n m xs
| n < 0 = takeFromTo 0 m xs
| m < n = []
takeFromTo _ _ [] = []
takeFromTo 0 0 (x:_) = [x]
takeFromTo 0 j (x:xs) = x : takeFromTo 0 (j - 1) xs
takeFromTo i j (_:xs) = takeFromTo (i - 1) (j - 1) xs
-- EXERCISE 05 ----------------------------------------------------------------
{-
5.1
- Define a recursive function which retains every third element in a list.
-}
eachThird :: [a] -> [a]
eachThird (_:_:x:xs) = x : eachThird xs
eachThird _ = []
-- alternative, more general solution
eachThird' :: [a] -> [a]
eachThird' = eachNth 3
eachNth :: Int -> [a] -> [a]
eachNth n = eachNthRec n n
eachNthRec :: Int -> Int -> [a] -> [a]
eachNthRec _ _ [] = []
eachNthRec n 1 (x:xs) = x : eachNthRec n n xs
eachNthRec n i (_:xs) = eachNthRec n (i - 1) xs
{-
5.2
- Define a recursive function which zips two lists in a crossing manner.
-}
crossZip :: [a] -> [b] -> [(a, b)]
crossZip (x:x':xs) (y:y':ys) = (x,y') : (x',y) : crossZip xs ys
crossZip _ _ = []