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 function pal/1
which generates all lists of palindromes. Hint: use only append
.