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
?-  encode_modified([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
X = [[4,a],b,[2,c],[2,a],d,[4,e]]
˜˜˜˜

* __Decode a run-length encoded list__.
Given a run-length code lis11. Construct its uncompressed version.

## Mini-Trabalho I: Estruturas de Dados

* Flat Lists: given a list with sub-lists _L_, write a program that generates a flat List _F_. As an example, given

~~~~{.prolog}
 L= [[],  [jim(z),  [2,  [b,  c]]],  [[]],  Z]].
F should unify with
1
 [jim(z), 2, b, c, Z].

  • 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
    1
     L= [1, 2, 3, 4, 5, 6].
    
    unify A with
    1
    [[1, 2], [3, 4], [5, 6]].