Exercise 4.1 How does Prolog respond to the following queries?
- [a,b,c,d] = [a,[b,c,d]].
- [a,b,c,d] = [a|[b,c,d]].
- [a,b,c,d] = [a,b,[c,d]].
- [a,b,c,d] = [a,b|[c,d]].
- [a,b,c,d] = [a,b,c,[d]].
- [a,b,c,d] = [a,b,c|[d]].
- [a,b,c,d] = [a,b,c,d,].
- [a,b,c,d] = [a,b,c,d|].
-  = _.
-  = [_].
-  = [_|].
Exercise 4.2 Which of the following are syntactically correct lists? If the representation is correct, how many elements does the list have?
Exercise 4.3 Write a predicate second(X,List) which checks whether X is the second element of List .
Exercise 4.4 Write a predicate swap12(List1,List2) which checks whether List1 is identical to List2 , except that the first two elements are exchanged.
Exercise 4.5 Suppose we are given a knowledge base with the following facts:
Write a predicate listtran(G,E) which translates a list of German number words to the corresponding list of English number words. For example:
Your program should also work in the other direction. For example, if you give it the query
it should return:
(Hint: to answer this question, first ask yourself “How do I translate the empty list of number words?”. That’s the base case. For non-empty lists, first translate the head of the list, then use recursion to translate the tail.)
Exercise 4.6 Write a predicate twice(In,Out) whose left argument is a list, and whose right argument is a list consisting of every element in the left list written twice. For example, the query
And the query
(Hint: to answer this question, first ask yourself “What should happen when the first argument is the empty list?”. That’s the base case. For non-empty lists, think about what you should do with the head, and use recursion to handle the tail.)
Exercise 4.7 Draw the search trees for the following three queries:
(Search trees were introduced in Chapter 2 .)