You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: problems/lists.org
+182Lines changed: 182 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,3 +92,185 @@ Success:
92
92
* 1.06 (*) Find out whether a list is a palindrome.
93
93
94
94
A palindrome can be read forward or backward; e.g. ["x","a","m","a","x"].
95
+
96
+
* 1.07.1 (**) Define a type to represent nested lists
97
+
98
+
Unlike its dynamically typed cousin, λProlog doesn't support simple heterogeneous
99
+
lists. Define a datatype =nlist A= that allows constructing possibly nested
100
+
lists of elements of type =A=.
101
+
102
+
An example, using =ls= for the constructor of nested lists and =el= to construct
103
+
single elements:
104
+
105
+
#+begin_src teyjus-edit
106
+
NestedList = ls [el 1, ls [el 2, ls [el 3, el 4], el 5]]
107
+
#+end_src
108
+
* 1.07.2 (**) Flatten a nested list structure.
109
+
110
+
Transform a list, possibly holding lists as elements, into a 'flat' list by
111
+
replacing each list with its elements (recursively).
112
+
113
+
Example:
114
+
115
+
#+begin_src teyjus-edit
116
+
> my_flatten (ls [el 1, ls [el 2, ls [el 3, el 4], el 5]]) X.
117
+
118
+
Success:
119
+
X = [1, 2, 3, 4, 5]
120
+
#+end_src
121
+
122
+
Hint: Use the predefined predicate =pred append i:list A, i:list A, o:list A=
123
+
124
+
* 1.08 (**) Eliminate consecutive duplicates of list elements.
125
+
126
+
If a list contains repeated elements they should be replaced with a single copy
127
+
of the element. The order of the elements should not be changed.
128
+
129
+
Example:
130
+
131
+
#+begin_src teyjus-edit
132
+
> compress [1,1,1,1,2,3,3,1,1,4,5,5,5,5] X.
133
+
134
+
Success:
135
+
X = [1,2,3,1,4,5]
136
+
#+end_src
137
+
138
+
* 1.09 (**) Pack consecutive duplicates of list elements into sublists.
139
+
140
+
If a list contains repeated elements they should be placed in separate sublists.
141
+
142
+
Example:
143
+
144
+
#+begin_src teyjus-edit
145
+
> pack [1,1,1,1,2,3,3,1,1,4,5,5,5,5] X.
146
+
147
+
Success:
148
+
X = [[1,1,1,1],[2],[3,3],[1,1],[4],[5,5,5,5]]
149
+
#+end_src
150
+
151
+
* 1.10 (*) Run-length encoding of a list.
152
+
153
+
Use the result of problem 1.09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as terms [N,E] where N is the number of duplicates of the element E.
Modify the result of problem 1.10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as [N,E] terms.
X = [many 4 "a", one "b", many 2 "c", many 2 "a", one "d", many 4 "e"]
179
+
#+end_src
180
+
181
+
* 1.12 (**) Decode a run-length encoded list.
182
+
183
+
Given a run-length code list generated as specified in problem 1.11. Construct
184
+
its uncompressed version.
185
+
186
+
* 1.13 (**) Run-length encoding of a list (direct solution).
187
+
188
+
Implement the so-called run-length encoding data compression method directly. I.e. don't explicitly create the sublists containing the duplicates, as in problem 1.09, but only count them. As in problem 1.11, simplify the result list by replacing the singleton terms [1,X] by X.
X = [many 4 "a", one "b", many 2 "c", many 2 "a", one "d", many 4 "e"]
197
+
#+end_src
198
+
199
+
* 1.14 (*) Duplicate the elements of a list.
200
+
201
+
Example:
202
+
#+begin_src teyjus-edit
203
+
> duplicate [1,2,3,4,5] X.
204
+
205
+
Success:
206
+
X = [1,1,2,2,3,3,4,4,5,5]
207
+
#+end_src
208
+
209
+
* 1.15 (**) Duplicate the elements of a list a given number of times.
210
+
211
+
Example:
212
+
213
+
#+begin_src teyjus-edit
214
+
> duplicate-n 3 [1,2,3] X.
215
+
216
+
Success:
217
+
X = [1,1,1,2,2,2,3,3,3]
218
+
#+end_src
219
+
220
+
What are the results of the goal:
221
+
222
+
#+begin_src teyjus-edit
223
+
> duplicate-n 3 X Y.
224
+
#+end_src
225
+
226
+
* 1.16 (**) Drop every N'th element from a list.
227
+
228
+
Example:
229
+
230
+
#+begin_src teyjus-edit
231
+
> drop [1,2,3,4,5,6,7,8] 3 X.
232
+
233
+
Success:
234
+
X = [1,2,4,5,7,8]
235
+
#+end_src
236
+
237
+
* 1.17 (*) Split a list into two parts; the length of the first part is given.
238
+
Do not use any predefined predicates.
239
+
240
+
Example:
241
+
242
+
#+begin_src teyjus-edit
243
+
> split 3 [1,2,3,4,5,6,7,8,9,10] L1 L2.
244
+
245
+
Success:
246
+
L1 = [1,2,3]
247
+
L2 = [4,5,6,7,8,9,10]
248
+
#+end_src
249
+
250
+
* 1.18 (**) Extract a slice from a list.
251
+
252
+
Given two indices, I and K, the slice is the list containing the elements between the I'th and K'th element of the original list (both limits included). Start counting the elements with 0.
253
+
254
+
Example:
255
+
256
+
#+begin_src teyjus-edit
257
+
> slice 4 8 [1,2,3,4,5,6,7,8,9,10] L.
258
+
259
+
Success:
260
+
L = [3,4,5,6,7]
261
+
#+end_src
262
+
263
+
* 1.19 (**) Rotate a list N places to the left.
264
+
Examples:
265
+
266
+
#+begin_src teyjus-edit
267
+
?- rotate ["a","b","c","d","e","f","g","h"] 3 X.
268
+
X = ["d","e","f","g","h","a","b","c"]
269
+
270
+
?- rotate ["a","b","c","d","e","f","g","h"] -2 X.
271
+
X = ["g","h","a","b","c","d","e","f"]
272
+
#+end_src
273
+
274
+
Hint: Use the predefined predicates =pred std.length i:list A, o:int= and
275
+
=pred append i:list A, i:list A, o:list A=, as well as the result of problem
0 commit comments