(Bratko, ex. 2.6)
Consider this program:
f(1, one). f(s(1), two). f(s(s(1)), three). f(s(s(s(X))), N) :- f(X, N).
What answer(s) will these queries produce?
f(s(1), A).
f(s(s(1))), two).
f(s(s(s(s(s(s(1))))))), C).
f(D, three).
Write a predicate second_last(X, L)
that is true if X is the next-to-last element of L.
Write a predicate next_to(X, Y, L)
that is true if elements X and Y appear in L, with Y immediately
after X.
Write a predicate even_length(L)
that
is true if the length of L is an even integer.
Write a predicate same_length(L, M)
that is true if L and M have the same length.
Write a predicate longer(L, M)
that
is true if L is longer than M. Do not use any numbers in your
solution.
Write a predicate double_length(L, M)
that is true if L is twice as long as M. Do not use any numbers in
your solution.
Write a predicate reverse(L, M)
that
is true if L and M contain
the same elements in reverse order.
When we rotate a list one element to the right, the last element moves to the first position and all other elements shift rightward.
Write a predicate rotate(L, M)
that
is true if M is L rotated one element to the right (or, equivalently,
L is M rotated one element to the left).
Write a predicate select(X, L, M)
that is true if X can be removed from L to make M. Equivalently,
select(X, L, M)
is true if X can be inserted anywhere in
M to make L.
Write a predicate select(X, L, Y, M)
that is true if L and M are identical except (possibly) for a single
element, which is X
in L and is Y in M.
Write a predicate all_same(L)
that is
true if all elements of L are identical.
Write a predicate all_diff(L)
that is
true if no two elements of L are identical.
Write a predicate zip(L, M)
that is
true if L is a list of pairs and M is a corresponding pair of lists,
where we represent pairs using the structure pair(X, Y)
.
For example, the following is true:
zip([pair(a, b), pair(c, d), pair(e, f)],
pair([a, c,
e
],
[b, d, f])).
Write a predicate numerals(L, M)
that
is true if L is a list of integers in the range 1 ≤ i ≤ 5 and M
is a corresponding list of atoms from the set [ one, two, three,
four, five ]. For example, numerals([2, 3, 2], [two,
three, two])
is true.
Write a predicate flatten(L, M)
that
is true if L is a list of lists and M is the concatenation of all
those lists. For eample, flatten([[a, b], [c], [d, e]], [a, b,
c, d, e])
is true.