This is the assignment that I have been given. I use a version called LPA Prolog for writing the code:
The following Prolog database records details of the friendships that exist between various individuals, as well as information about their respective interests.
friends( john, [fred,sue,george] ). % fred, sue & george are friends of john
friends( fred, [sue] ).
friends( sue, [fred,george] ).
friends( george, [ ] ). % george has no friends
male( john ). % john is male
male( fred ).
male( george ).
female( sue ).
interests( john, [sport] ). % john is interested in sport
interests( fred, [music] ).
interests( george, [sport,music] ). % george is interested in sport and music
interests( sue, [music] ).
Note that friendship is not a symmetrical relationship. for example, within this database, Fred is a friend of John – but John is not a friend of Fred.
Write and test the following Prolog programs.
a) all_friends/3 takes the names of two individuals (arguments 1 & 2) and finds the list (argument 3) of all their friends – e.g.
?- all_friends( john, sue, FRIENDS ).
FRIENDS = [fred,sue,george,fred,george] (10%)
b) common_friend/3 finds the names of two individuals (arguments 1 & 2) who have at least one friend (argument 3) in common – e.g.
?- common_friend( P1, P2, F ).
P1 = john
P2 = fred
F = sue (10%)
c) chums/2 finds the names of two individuals who are friends of one another (i.e. argument 1 is a friend of argument 2 and vice-versa) – e.g.
?- chums( john, fred ).
no
?- chums( C1, C2 ).
C1 = fred
C2 = sue (10%)
d) buddies/2 finds the names of two individuals who are friends of one another (i.e. argument 1 is a friend of argument 2 and vice-versa) and who also share a common interest – e.g.
?- buddies( sue, BUDDY ).
BUDDY = fred (10%)
e) add_friend/2 updates the database by adding argument 2 as a friend of argument 1, and prints out the new list of friends as confirmation – e.g.
?- add_friend( fred, john ).
freds friends are [john,sue] (10%)
f) update/1 displays a menu that offers the user three options: i) add to someone’s list of friends. ii) add to someone’s list of interests. iii) quit. The menu should be redisplayed until the user selects the quit option – e.g.
?- update(george).
Please choose an option.
1. add a friend
2. add an interest
3. quit
|: 1.
who is new friend?
|: fred.
georges friends are [fred]
Please choose an option.
1. add a friend
2. add an interest
3. quit
etc. (20%)