在序言中返回一个列表 [英] Returning a list in prolog
问题描述
我想问一个关于返回列表的问题...事实:
i wanna ask a question about returning a list... Facts:
团队(团队名称、董事、国籍、总体目标)
TEAM(TEAMNAME,DIRECTOR,NATIOANALITY,OVERALLGOAL)
team (milan,allegri,italy, 8.5).
team (inter,benitez,italy,7.6).
team (barcelona,guardiola,spain,7.8).
team (realmadrid,mourinho,spain,7.2).
我想创建一个谓词:find(T,N,G) : T 是团队名称,N 是团队的国籍,这个团队的总目标必须大于 G.输出必须是这样的:
and i want to create a predicate: find(T,N,G) : T is name of team, N is nationality of team and this team's overallgoal must be greater than G. and outputs must be like these:
find([], spain,9).返回真
find(X, spain,6).返回 X=[巴塞罗那,皇家马德里]
我尝试这样做:
find(T,N,G):-find1(T,N,G),is_set(T).
find1([]).
find1([T|Ts],N,G):-team(T,_,N,Gs),Gc>G,find1(Ts).
它给出了结果,但不像上面的输出......如果我的目标是 find([],spain,9).然后给假...如果我的目标是 find(X,spain,6).然后给出第一个 X=barcelona 并等待;"之后给 X=realmadrid ......但我想要一个像上面那样的列表......
it gives results but not like output above... if my goal is find([],spain,9). then give false... if my goal is find(X,spain,6). then give first X=barcelona and wait for ";" after that give X=realmadrid... but i want to a list like above...
非常感谢...
推荐答案
要从子句数据库中提取满足谓词的项目列表,应使用 findall
谓词.例如,您的代码可以重写如下:
To extract a list of items satisfying a predicate from a database of clauses, one should use findall
predicate. For example, your code could be rewritten as follows:
find(T, N, G) :- findall(X, (team(X, _, N, G0), G0 > G), T).
这篇关于在序言中返回一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!