1
votes
% A quiz team structure takes the form:
% team(Captain, Vice_captain, Regular_team_members).
% Captain and Vice_captain are player structures;
% Regular_team_members is a list of player structures.
% player structures take the form:
% player(First_name, Surname, details(Speciality,Recent_score)).

I've been given the following Prolog database:

team(player(niall,elliott,details(history,11)),
     player(michelle,cartwright,details(fashion,19)),
     [player(peter,lawlor,details(science,12)),
      player(louise,boyle,details(current_affairs,17))
     ]
    ).

What would be the code needed to get the firstname and the recent score of all players whose recent score is above 15?

I've tried using exists but it keeps giving me errors.

Second question:

I need to get the surname of any vice-captain whose team includes a captain or a regular team member whose speciality is science.

I can get the surname of the vice-captains by using the first line below, but the second part is more tricky.

part_two(Surname):-
team(_,player(_,Surname,_),_),
Regular_player = team(_,_,player(_,_,details(science,_))),
Captain = team(player(_,_,details(science,_),_,_)).
1
Please share your code, we may be able to help you find the flaw in your reasoning.Daniel Lyons

1 Answers

2
votes

A more detailed description of what you tried and how it didn't work would be better because (a) some people are reluctant to do your homework for you, and (b) we can better clear up your misunderstandings if we know what those misunderstandings are.

Anyway, Prolog programming is all about decomposing problems.

The first problem is to find out which players exist at all. A player is a team captain or a team vice captain or a regular team member. This definition has three parts separated by "or", which suggests that we need a predicate composed of three clauses:

player(Captain) :-
    team(Captain, _, _).
player(Vice_captain) :-
    team(_, Vice_captain, _).
player(Regular_player) :-
    team(_, _, Regular_members),
    member(Regular_player, Regular_members).

We can test this:

?- player(P).
P = player(niall, elliott, details(history, 11)) ;
P = player(michelle, cartwright, details(fashion, 19)) ;
P = player(peter, lawlor, details(science, 12)) ;
P = player(louise, boyle, details(current_affairs, 17)).

Now we want to identify "good players". You wrote that you have "tried using exists". There is no exists in Prolog, and it isn't needed. In order to express something like "there exists a player P such that ...", we just define a predicate containing the goal player(P) and some other goals expressing the property we are interested in. This leads to a definition like this:

good_player(First_name, Recent_score) :-
    player(P),
    P = player(First_name, _, details(_, Recent_score)),
    Recent_score > 15.

You can read this as "there is a player P with first name First_name and recent score Recent_score such that the recent score is greater than 15".

?- good_player(F, S).
F = michelle,
S = 19 ;
F = louise,
S = 17.