This is an old revision of the document!


Generative predicates

1. Write the predicate sublist/2 which constructs each sublist of a given list. Hint: use append.

?- sublist([7,2,9],R).
% R will subsequently be bound to [], [7], [2], [9], [7,2], [2,9], [7,2,9] 

2. Rewrite the predicate such that the empty list is reported only once.

3. Write the predicate natlist/1 which generates each finite list of natural number in ascending order.

?- natlist(R).
R = [0] ;
R = [0,1] ;
R = [0,1,2] ; 
...

4. Write the predicate oddOnly/2 which removes all even integers from a list.

?- oddOnly([1,2,3,4],R).
R = [2,4].

5. Write the predicate oddList/1 which generates all finite lists of odd numbers, in ascending order.

6. Write a predicate eqelem/1 which generates all lists of uninstantiated variables which must be equal. Example:

?- eqelem(L), length(L,3), L=[0|_].
L = [0,0,0].

7. Use the previous predicate to write repeat/3 which returns a list containing the same value X repeated K times.

8. Write a predicate pal/1 which generates all lists of palindromes. Hint: use only append.

9. Write a predicate ksubset/3 where ksubset(C,K,V) generates all sets (represented as lists) C with K elements from the list V. Hint: build the recursion scheme after K.

10. Write a predicate subset where subset(C,V) generates all subsets of V. Hint: build the recursion scheme after each element in V.

11. What is the difference between subset(C,V) and ksubset(C,N,V)? where N is the number of elements in V. Write your answer down.

12. Implement kvertexcover/3 where kvertexcover(C,K,G) generates all K-coverings in G. G=[V,E] where V is a set of nodes and E is a set of undirected edges. Hint: use negation to express a universal constraint.

13. Implement kclique/3.

14. Implement connected/4, where connected(X,Y,P,G) generates all paths P between nodes X and Y in graph G.

14. Implement ham/2 where ham(P,G) generates all hamiltonian paths in G.