Write a predicate triple(L)
that is
true if L is a list with three elements, all of which are identical.
Write a predicate triple_diff(L)
that
is true if L is a list with three elements, all of which are
different.
Write a predicate all_same(L) that is true if all elements of L are the same.
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 is_list(L)
that is
true if L is a list of any length.
Write a predicate same_length(L, M)
that is true if L and M have the same length. Do not use any integers
in your solution.
Write a predicate even_length(L)
that
is true if the length of L is an even integer.
Write a predicate longer(L, M)
that
is true if L is longer than M.
Write a predicate double_length(L, M)
that is true if L is twice as long as M.
Write a predicate len(L, N)
that is
true if the length of list L is N.
Write a predicate all_diff(L)
that is
true if all elements of L are all different.
Write a predicate ordered(L)
that is
true if the integers in L are in non-decreasing order.
Write a predicate nth(N, L, X)
that
is true if the nth element of list L is X, where the first element
has index 0.
Write a predicate prefix(L, M)
that is true if L is a prefix of list M.
Write a predicate reverse(L, M)
that
is true if L and M contain
the same elements in reverse order.
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 product(L, N)
that
is true if N is the product of the integers in L.
Write a predicate reduce_to_zero(L)
that takes L, a list of integers. Given integers x1, ...,
xn, the predicate should succeed if there is any sequence
op1, ..., opn - 1 of arithmetic operations from
the set (+, -, *, /) such that x1 op1 x2
op2 x3 ... xn = 0. Assume that all
operators are right-associative. For example, reduce_to_zero([8,
8, 5, 4])
will succeed because (for example) 8 - (8 * (5 - 4))
= 0.
Modify the predicate from the previous exercise so
that takes an additional argument holding a list of operations, where
the atoms add, sub, mul and div represent the operations. For
example, reduce_to_zero([8, 8, 5, 4], [sub, mul, sub])
should succeed.
Write a predicate gcd(I, J, K)
that
is true if the greatest common divisor of I and J is K.
Write a predicate is_prime(N)
that is
true if N is prime.
Write a predicate all_primes(I, J)
that returns a list of all prime numbers between I and J, inclusive.
Write a predicate smallest_factor(N, P)
that is true if P
is the smallest prime factor of N
.
Suppose that we'd like to fill in a 3 x 3 grid with letters so that every row and column contains one of these words:
AGE, AGO, CAN, CAR, NEW, RAN, ROW, WON
Write a Prolog program that can find all possible solutions.
Consider the following tiny Minesweeper board of dimensions 5 x 2:
A number N means that there are N mines in adjacent squares (which may be adjacent horizontally or diagonally).
Write a Prolog program that can find all possible positions for the mines.
Someone has stolen Beethoven's wig and has put it in one of four locked boxes. The boxes are numbered 1,2,3,4 in that order. There are four different keys that each have their own color. Also:
The green key goes to the third or fourth box.
The wig is to the left of the fourth box.
The wig is to the right of the first box.
The yellow key is to the left of the wig.
The blue key is to the right of the yellow key and to the left of the green key.
The red key goes to the first box.
Which box contains Beethoven's wig? Write a Prolog program that can find the answer.
Consider this acyclic directed graph:
One way to represent a graph in Prolog is as a list of edges. For example, we can represent the graph above by this term:
G = [ [a, b], [a, e], [b, c], [b, f], [e, d], [e, f], [f, c], [g, d], [g, h], [h, f] ].
Write a predicate path(G, V, W, L) that is true if L is a list of vertices along a path from V to W in the graph G.