Material de Apoio
Problemas sobre listas
-
Find the last element of a list. Example:
1 2
?- my_last(X,[a,b,c,d]). X = d
-
Find the K'th element of a list. The first element in the list is number 1. Example:
1 2
?- element_at(X,[a,b,c,d,e],3). X = c
-
Eliminate consecutive duplicates of list elements. If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed. Example:
1 2
?- compress([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X). X = [a,b,c,a,d,e]
-
Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements they should be placed in separate sublists. Example:
1 2
?- pack([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X). X = [[a,a,a,a],[b],[c,c],[a,a],[d],[e,e,e,e]]
-
Run-length encoding of a list Use the result of problem P09 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. Example:
1 2
?- encode([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X). X = [[4,a],[1,b],[2,c],[2,a],[1,d][4,e]]
-
Modified run-length encoding Modify the previous program 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. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 |
|
- Given a N-dimensional array with sizes N1,...,Nk and a flat list of size N1...Nk unify A with a list of lists representing the array. Say, given the dimensions [3,2] , given
unify A with1
L= [1, 2, 3, 4, 5, 6].
1
[[1, 2], [3, 4], [5, 6]].