ASSIGNMENT 1. Due November 2.
---------------------------------------------------------
Program from the book (Queen Victoria's family):
male(albert).
male(edward).
female(alice).
female(victoria).
parents(edward,victoria,albert).
parents(alice,victoria,albert).
sister_of(X,Y):-
female(X),
parents(X,M,F),
parents(Y,M,F).
=========================================================
1. Exercise 1.2 from the book:
When the sister_of rule is applied to the database of part of Queen
Victoria's family discussed previously, more that one answer can be
obtained. Explain how all the answers can be obtained, and what they
are.
=========================================================
2. Exercise 1.3 from the book:
This exercise has been inspired by one in Robert Kowalski's book
Logic for Problem Solving, published by North Holland in 1979.
Suppose someone has already written Prolog clauses that define the
following relationships:
father(X,Y) /* X is a father of Y */
mother(X,Y) /* X is the mother of Y */
male(X) /* X is male */
female(X) /* X is female */
parent(X,Y) /* X is a parent of Y*/
diff(X,Y) /* X and Y are different */
The problem is to write Prolog clauses to define the following
relationships:
is_mother(X) /* X is a mother */
is_father(X) /* X is a father */
is_son(X) /* X is a son */
sister_of(X,Y) /* X is a sister of Y */
granpa_of(X,Y) /* X is a grandfather of Y */
sibling(X,Y) /* X is a sibling of Y */
For example, we could write a rule for aunt, provided we were
supplied with (or wrote) rules for female, sibling, and parent.
aunt(X,Y):-female(X),sibling(X,Z),parent(Z,Y).
This could also be written:
aunt(X,Y):-sister_of(X,Z),parent(Z,Y).
provided that we wrote the sister_of rule.
==========================================================
3. Exercise 1.4 from the book:
Using the sister_of rule defined in the text, explain why it is
possible for some object to be her own sister. How would you change
the rule if you did not want this property? Hint: assume that the
predicate diff of Exercise 1.3 is already defined.
===========================================================
4. Exercise 2.1 from the book:
Say whether the following goals would succeed, and which variables, if
any, would be instantiated to what values:
pilots(A,london) = pilots(london,paris).
point(X,Y,Z) = point(X1,Y1,Z1).
letter(C) = word(letter).
noun(alpha) = alpha.
'vicar' = vicar.
f(X,X) = f(a,b).
f(X,a(b,c)) = f(Z,a(Z,c)).
===========================================================
5. Write a predicate that accepts a list and succeeds if that list has exactly three elements.
===========================================================
6. Write a predicate that accepts a list and generates from it a similar
list with the first two elements swapped.
It should work like this:
?-swap_first_two([a,b,c,d], [b,a,c,d]).
Yes
?-swap_first_two([a,b,c,d], X).
X= [b,a,c,d];
No
?-swap_first_two(X, [b,a,c,d]).
X=[a,b,c,d];
No
?-swap_first_two([a,b,c,d], [b,a,d,c]).
No
?-swap_first_two([a,b,c,d], [X,Y,c,d]).
X=b
Y=a;
No
============================================================
7. Define the relation reverse_list(List, Reversed_List) that
reverses lists, e.g.:
?- reverse_list([a,b,c,d],[d,c,b,a]).
yes
?- reverse_list([a,b,c,d],X).
X=[d,c,b,a];
No
============================================================
8. Define the predicate palindrome(List) that recognizes palindromes.
A list is a palindrome if it reads the same in the forward and in
the backward direction. For example:
?- palindrome([m,a,d,a,m]).
yes
?- palindrome([s,i,r]).
No
=============================================================
9. Define the relation shift(List1, List2) so that List2 is List1
"shifted rotationally" by one element to the left. For example,
?- shift([1,2,3,4,5], L1), shift(L1,L2).
L1=[2,3,4,5,1]
L2=[3,4,5,1,2];
No
=============================================================
10. Define the relation maximum(List,Integer) that succeeds if Integer
is the maximal element of the list of integers List. Example,
?- maximum([1,2,3,4,2,-1,4],Max).
Max=4;
No
?- maximum([],Max).
No