Week 2: Exercises

1. Triple List

Write a predicate triple(L) that is true if L is a list with three elements, all of which are identical.

2. Triple Diff

Write a predicate triple_diff(L) that is true if L is a list with three elements, all of which are different.

3. Last Element

Write a predicate last_elem(X, L) that is true if X is the last element of the list L.

4. Second Last

Write a predicate second_last(X, L) that is true if X is the next-to-last element of L.

5. Next To

Write a predicate next_to(X, Y, L) that is true if elements X and Y appear in L, with Y immediately after X.

6. Even Length

Write a predicate even_length(L) that is true if the length of L is an even integer.

7. Same Length

Write a predicate same_length(L, M) that is true if L and M have the same length.

8. Longer

Write a predicate longer(L, M) that is true if L is longer than M.

9. Double Length

Write a predicate double_length(L, M) that is true if L is twice as long as M.

10. Is A List

Write a predicate is_list(L) that is true if L is a list of any length.

11. Length

Write a predicate len(L, N) that is true if the length of list L is N.

12. All Same

Write a predicate all_same(L) that is true if all elements of L are identical.

13. All Different

Write a predicate all_diff(L) that is true if all elements of L are all different.

14. Integer Sum

Write a predicate sum(L, N) that is true if N is the sum of the integers in the list L.

15. Real Sum

Write a predicate sum(L, R) that is true if R is the floating-point sum of the numbers in the list L.

16. Ordered List

Write a predicate ordered(L) that is true if the floating-point numbers in L are in non-decreasing order.

17. Member

Write a predicate mem(X, L) that is true if X is a member of the list L.

18. Nth

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.

19. Append

Write a predicate append(L, M, N) that is true if the lists L and M can be appended to form N.

20. Reverse

Write a predicate reverse(L, M) that is true if L and M contain the same elements in reverse order.

21. Rotate

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).

22. Select

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.

23. Select, Extended

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.

24. List of Numbers

Write a predicate num_list(I, J, L) that is true if L is the list containing integers I through J, inclusive. If J < I, then L should be the empty list.

25. Slice

Write a predicate slice(L, I, J, M) that is true if M contains elements I .. J of L, where elements are indexed starting from 0. For example, slice([r, i, v, e, r], 1, 3, [i, v, e]) is true.

26. Greatest Common Divisor

Write a predicate gcd(I, J, K) that is true if the greatest common divisor of I and J is K.

27. Prime

Write a predicate is_prime(N) that is true if N is prime.

28. All Primes

Write a predicate all_primes(I, J) that returns a list of all prime numbers between I and J, inclusive.

29. Smallest Prime Factor

Write a predicate smallest_factor(N, P) that is true if P is the smallest prime factor of N.

30. Beethoven's Wig

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:

  1. The green key goes to the third or fourth box.

  2. The wig is to the left of the fourth box.

  3. The wig is to the right of the first box.

  4. The yellow key is to the left of the wig.

  5. The blue key is to the right of the yellow key and to the left of the green key.

  6. The red key goes to the first box.

Which box contains Beethoven's wig? Write a Prolog program that can find the answer.

31. Acyclic Directed Graph

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.

32. Cyclic Directed Graph

Let's modify the graph from the previous exercise by adding an edge from f back to a, so that the graph is now cyclic:

G = [ [a, b], [a, e], [b, c], [b, f], [e, d],
      [e, f], [f, c], [g, d], [g, h], [h, f],
      [f, a] ].

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 G, such that your predicate can always find all paths between given vertices V and W.