-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPilecki Michał 4_2.hs
More file actions
225 lines (166 loc) · 4.53 KB
/
Pilecki Michał 4_2.hs
File metadata and controls
225 lines (166 loc) · 4.53 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<<<<<<< HEAD
module Lists where
import Data.Char(isLower)
{- Task 1 *
1. Analyze the function call sum1 [3,5,7].
2. Analyze the function call sum1 [3,5,7,9].
Follow the examples in silnia.pps and on the site:
http://www.balois.pl/jipp/haskell/listy.htm
-}
sum1 :: Num a => [a] -> a
sum1 [] = 0
sum1 (x:xs) = x + sum1 xs
{-
Analiza wywo�ania sum1 [3,5,7]:
1. sum1 [3,5,7]
= 3 + sum1 [5,7] -- pierwszy element 3, przekazujemy reszt� listy [5,7]
2. sum1 [5,7]
= 5 + sum1 [7] -- pierwszy element 5, przekazujemy reszt� listy [7]
3. sum1 [7]
= 7 + sum1 [] -- pierwszy element 7, przekazujemy reszt� listy []
4. sum1 []
= 0 -- warunek bazowy, zwracamy 0
5. Teraz sumujemy wyniki:
= 7 + 0 = 7
= 5 + 7 = 12
= 3 + 12 = 15
Ostateczny wynik: 15
-}
{-
Analiza wywo�ania sum1 [3,5,7,9]:
1. sum1 [3,5,7,9]
= 3 + sum1 [5,7,9]
2. sum1 [5,7,9]
= 5 + sum1 [7,9]
3. sum1 [7,9]
= 7 + sum1 [9]
4. sum1 [9]
= 9 + sum1 []
5. sum1 []
= 0
6. Teraz sumujemy wyniki:
= 9 + 0 = 9
= 7 + 9 = 16
= 5 + 16 = 21
= 3 + 21 = 24
Ostateczny wynik: 24
-}
-- 4.2.2 zdefiniuj funkcj� sum2, kt�ra jako argument przyjmuje list� liczb i zwraca sum� element�w o indeksach parzystych
sum2 [] = 0
sum2 (x:_:xs) = x + sum2 xs
sum2 (x:[]) = x
{-
ghci> sum2 [1]
1
ghci> sum2 [1,2]
1
ghci> sum2 [1,2,3]
4
-}
-- 4.2.3 funkcj� sum3, kt�ra jako argument przyjmuje list� liczb i zwraca sum� element�w o indeksach 3, 6, 9, �;
sum3 [] = 0
sum3 [_] = 0
sum3 [_, _] = 0
sum3 (_:_:x:xs) = x + sum3 xs
{-
ghci> sum3 []
0
ghci> sum3 [1]
0
ghci> sum3 [1,2]
0
ghci> sum3 [1,2,3]
3
-}
{- Task 3 *
Analyze the function call countLower "Bob".
Follow the examples in silnia.pps and on the sites:
https://balois.pl/jipp/haskell/listy.htm
https://balois.pl/jipp/haskell/fold.htm
-}
{-
Analiza wywo�ania countLower "Bob":
1. Rozbijamy ci�g znak�w na list�:
"Bob" � ['B', 'o', 'b']
2. Filtrujemy ma�e litery za pomoc� filter isLower:
- 'B' � nie jest ma�� liter� � odrzucamy
- 'o' � jest ma�� liter� � zostaje
- 'b' � jest ma�� liter� � zostaje
Wynik: ['o', 'b']
3. Liczymy d�ugo�� przefiltrowanej listy:
length ['o', 'b'] � 2
Wynik ko�cowy:
countLower "Bob" = 2
-}
--4.3 Zdefiniuj funkcj� countLowerUpper, wyznaczaj�c� liczb� ma�ych i wielkich liter w przekazanym jej tek�cie wej�ciowym. Funkcja powinna by� nast�puj�cego typu countLowerUpper :: String � (Int, Int).
countLowerUpper :: String -> (Int, Int)
countLowerUpper [] = (0, 0)
countLowerUpper (x:xs) | 'a' <= x && x <= 'z' = (l + 1, u)
| 'A' <= x && x <= 'Z' = (l, u + 1)
| otherwise = (l, u)
where (l, u) = countLowerUpper xs
{-
countLowerUpper ""
(0,0)
ghci> countLowerUpper "Ala mA KoTa"
(5,4)
-}
-- 4.4
{-
(*) Zdefiniuj funkcj� string2bools, kt�ra list� znak�w zast�puje list� warto�ci logicznych okre�laj�cych, czy oryginalny element by� ma�� liter�, czy te� nie. Funkcj� umie�� w module Lists.
Wskaz�wka:
Do modu�u Lists poni�ej s�owa where dodaj poni�sz� lini�:
import Data.Char
-}
string2bools :: String -> [Bool]
string2bools = map isLower
{-
ghci> string2bools ""
[]
ghci> string2bools "Hello"
[False,True,True,True,True]
ghci> string2bools "ABCxyz"
[False,False,False,True,True,True]
ghci> string2bools "123aB!"
[False,False,False,True,False,False]
-}
{- Task 4 *
Analyze the function call gtx 1 [2,1,3].
Follow the examples in silnia.pps and on the sites:
https://balois.pl/jipp/haskell/listy.htm
https://balois.pl/jipp/haskell/fold.htm
-}
{-
1. Rozwijamy wywo�anie countLower "Bob":
countLower "Bob"
= length (filter isLower "Bob")
2. Funkcja filter isLower "Bob":
"Bob" � ['B', 'o', 'b']
- 'B' � NIE jest ma�� liter� � odrzucamy
- 'o' � JEST ma�� liter� � zostaje
- 'b' � JEST ma�� liter� � zostaje
Wynik dzia�ania filter:
= "ob" (lista ['o', 'b'])
3. Teraz obliczamy d�ugo�� wyniku:
length "ob" = 2
Ostateczny wynik:
countLower "Bob" = 2
-}
-- 4.5
{-
(*) funckja ltx list� element�w z listy wej�ciowych mniejszych lub r�wnych x.
(*) funkcja cltx liczb� element�w listy mniejszych od x;
-}
ltx :: Ord a => a -> [a] -> [a]
ltx x xs = [y | y <- xs, y <= x]
cltx :: Ord a => a -> [a] -> Int
cltx x xs = length [y | y <- xs, y < x]
{-
ghci> ltx 5 [3,7,1,5,9,4]
[3,1,5,4]
ghci> ltx 10 [15,8,20,3]
[8,3]
ghci> cltx 5 [3,7,1,5,9,4]
3
ghci> cltx 10 [15,8,20,3]
2