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

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

I've been given the database above (I haven't copied over all the entries of people as it would be too long).

I've been asked 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 using the code below but I can't return only those teams that include a captain or regular team members whose speciality is science. What would I need to add to do this?

part_two(Surname):-
    team(_,player(_,Surname,_),_).

I've also been asked to also get the first name and the surname of any captain whose regular team members number more than one and who all have the same surnames.

This is my attempt so far:

part_three(First_name,Surname):-
    team(Captain,_,Regular_team_members),
    first_name(Captain,First_name),
    surname(Captain,Surname),
    Regular_team_members=[_,_|_].

I just need to exclude the details of those captains whose regular team members don't all have the same surname.

2
If the first 2 in a team are captain and vice captain automatically, how can a team not contain a captain?BitTickler
@BitTickler I just skipped over the details of captain as I only needed the details of vice-captain.Dazzler95

2 Answers

3
votes
part_two(Surname):-
    team(Captain, Vice_captain, Regular_team_members),
    surname(Vice_captain, Surname),
    member(Player, [Captain|Regular_team_members]),
    specialty(Player, science).

% 'abstract data structures' accessors
surname(player(_First_name, Surname, _Details), Surname).
specialty(player(_First_name, _Surname, details(Speciality, _Recent_score)), Speciality).

Since you're going anyway to scan the Regular_team_members list, looking for appropriate constraint, you can get a simpler 'program' first 'joining' the Captain to other players.

1
votes

You could change a little what you have already written as follows:

 part_two(Surname):-
    team(P,player(_,Surname,_),L), 
    ( P=player(_,_,details(science,_)) -> true ; member(player(_,_,details(science,_)),L) ).

Example:

Database:

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


team(player(niall1,elliott1,details(science,11)),
     player(michelle1,cartwright1,details(fashion,19)),
          [player(peter,lawlor,details(history,12)),
          player(louise,boyle,details(current_affairs,17))]).

team(player(niall2,elliott2,details(history,11)),
     player(michelle2,cartwright2,details(fashion,19)),
          [player(peter,lawlor,details(science,12)),
          player(louise,boyle,details(current_affairs,17))]).



 team(player(niall3,elliott3,details(science,11)),
     player(michelle3,cartwright3,details(fashion,19)),
          [player(peter,lawlor,details(science,12)),
          player(louise,boyle,details(current_affairs,17))]).

Now querying:

?- part_two(X).
X = cartwright1 ;
X = cartwright2 ;
X = cartwright3.