-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture-07.hs
185 lines (154 loc) · 4.43 KB
/
lecture-07.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
174
175
176
177
178
179
180
181
182
183
184
185
{-|
Module : Lecture7Exercises
Description : Solutions to Lecture 7 exercises
Maintainer : Dinko Osrecki
-}
module Lecture7Exercises where
import Control.Arrow
import Data.Tuple
-- EXERCISE 01 ----------------------------------------------------------------
{-
1
- Define the functions using partial application of existing functions.
1.1 a
- Define a function which takes first three elements from the list.
-}
takeThree :: [a] -> [a]
takeThree = take 3
{-
1.1 b
- Define a function which drops first three elements from the list.
-}
dropThree :: [a] -> [a]
dropThree = drop 3
{-
1.1 c
- Define a function which takes one element and repeats it 100 times
in a list.
-}
hundredTimes :: a -> [a]
hundredTimes = replicate 100
{-
1.2 a
- Define a function which indexes the elements in a list.
-}
index :: (Num b, Enum b) => [a] -> [(b,a)]
index = zip [0..]
{-
1.2 b
- Write the function which does the same, but with index at the 2nd position.
-}
index' :: (Num b, Enum b) => [a] -> [(a,b)]
index' = zipWith swap' [0..]
where swap' = curry swap
{-
1.3
- Define a function which returns a string of length 'n' consisting of
characters '='.
-}
divider :: Int -> String
divider = replicate' '='
where replicate' = flip replicate
-- EXERCISE 02 ----------------------------------------------------------------
{-
2.1 a
- Define a function which applies a binary function on the last elements of
two lists.
-}
applyOnLast :: (a -> b -> c) -> [a] -> [b] -> c
applyOnLast f xs ys = f (last xs) (last ys)
{-
2.1 b
- Using 'applyOnLast' and 'addThree' define a function which adds last
elements of the lists and 100.
-}
lastTwoPlus100 :: (Num a) => [a] -> [a] -> a
lastTwoPlus100 = applyOnLast (addThree 100)
addThree :: Num a => a -> a -> a -> a
addThree a b c = a + b + c
{-
2.2 a
- Define a function which applies 'n' times function 'f' to argument 'x'.
If n <= 0, return 'x' unaltered.
-}
applyManyTimes :: (Num a, Ord a) => a -> (b -> b) -> b -> b
applyManyTimes n f x
| n <= 0 = x
| otherwise = applyManyTimes (n - 1) f (f x)
{-
2.2 b
- Using 'applyManyTimes' define 'applyTwice'.
-}
applyTwice :: (a -> a) -> a -> a
applyTwice = applyManyTimes 2
-- EXERCISE 03 ----------------------------------------------------------------
{-
3
- Define the following functions using 'map'.
3.1
- Define a function which turns a list into list of lists containing each
element.
-}
listifylist :: [a] -> [[a]]
listifylist = map (:[])
{-
3.2
- Define a function which cuts off all values from the list at the value 'n'.
-}
cutoff :: (Num a, Ord a) => a -> [a] -> [a]
cutoff n = map (min n)
-- EXERCISE 04 ----------------------------------------------------------------
{-
4
- Define the following functions using 'map' and 'filter'.
4.1
- Define a function which adds the squares of all even numbers from a list.
-}
sumEvenSquares :: (Integral a) => [a] -> a
sumEvenSquares = sum . map (^ 2) . filter even
{-
4.2
- Define a function which counts how many times given element occurs in
the list.
-}
freq :: Eq a => a -> [a] -> Int
freq x = length . filter (== x)
{-
4.3
- Define a function which filters all elements that occur at least 'n'
times in a list.
-}
freqFilter :: Eq a => Int -> [a] -> [a]
freqFilter n xs = filter (\x -> freq x xs >= n) xs
-- EXERCISE 05 ----------------------------------------------------------------
{-
5
- Define the following functions using lambda expressions.
5.1
- Define a function which given a list, filters all elements that fall
within the specified interval.
-}
withinInterval :: (Num a, Ord a) => a -> a -> [a] -> [a]
withinInterval n m = filter (\x -> n <= x && x <= m)
{-
5.2
- Define a function which returns the second column of the matrix, encoded
as a list of lists.
-}
sndColumn :: [[a]] -> [a]
sndColumn = map (\row -> row !! 1)
sndColumn' :: [[a]] -> [a]
sndColumn' = map (!! 1)
{-
5.3
- Define a function which takes a list of pairs and returns a list of the
pairs so that the first element in the pair is smaller than the second one.
If elements are equal, the pair is discarded.
-}
canonicalizePairs :: Ord a => [(a, a)] -> [(a, a)]
canonicalizePairs = map (\x -> (min' x, max' x)) . filter (\(a,b) -> a /= b)
where
min' = uncurry min
max' = uncurry max
canonicalizePairs' :: Ord a => [(a, a)] -> [(a, a)]
canonicalizePairs' = map (uncurry min &&& uncurry max) . filter (uncurry (/=))