% 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))]).我已经得到了上面的数据库(我没有复制所有人的条目,因为它太长了)。
我被要求知道任何一位副队长的姓氏,他的团队包括一名队长或一名擅长科学的普通队员。
我可以使用下面的代码获得副队长的姓氏,但我不能只返回那些包括队长或擅长科学的常规团队成员的团队。我需要添加什么才能做到这一点?
part_two(Surname):-
team(_,player(_,Surname,_),_).我还被要求知道任何一位队长的名字和姓氏,这些队长的正式队员超过一人,而且都有相同的姓氏。
到目前为止,这是我的尝试:
part_three(First_name,Surname):-
team(Captain,_,Regular_team_members),
first_name(Captain,First_name),
surname(Captain,Surname),
Regular_team_members=[_,_|_].我只需要排除那些常规团队成员并不都有相同姓氏的队长的详细信息。
发布于 2017-01-13 19:03:08
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).因为你无论如何都要扫描Regular_team_members列表,寻找合适的约束,你可以得到一个更简单的“程序”,首先“加入”Captain到其他玩家。
发布于 2017-01-13 18:50:53
您可以对已经编写的代码稍作修改,如下所示:
part_two(Surname):-
team(P,player(_,Surname,_),L),
( P=player(_,_,details(science,_)) -> true ; member(player(_,_,details(science,_)),L) ).示例:
数据库:
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))]).现在查询:
?- part_two(X).
X = cartwright1 ;
X = cartwright2 ;
X = cartwright3.https://stackoverflow.com/questions/41632532
复制相似问题