如果我有一个人的知识库和他们的生日,比如:
birthYear( adam , 2000 ).
birthYear( bob , 2001 ).
birthYear( john , 2002 ).
如何创建一个查询,在不修改知识库的情况下,详尽地搜索知识库,找到最年轻的人?
发布于 2022-09-22 08:22:42
可以使用全
person_year(adam, 2000).
person_year(bob, 2001).
person_year(john, 2002).
youngest_person(Person) :-
% Find lowest year
aggregate_all(min(Y), person_year(_P, Y), Year),
% Lookup person from lowest year
person_year(Person, Year).
结果在swi-prolog:
?- youngest_person(Person).
Person = adam.
发布于 2022-09-22 18:50:45
像这样吗?
youngest(P) :-
findall( P:Y , birth_year(P,Y) , PYs ),
most_recent_year( PYs, Y ),
member(P:Y,Ps)
.
most_recent_year( [_:Y|PYs], R ) :- most_recent_year(PYs,Y,R) .
most_recent_year( [] , R , R ) .
most_recent_year( [_:Y|PYs] , T , R ) :- Y > T , ! , most_recent_year(PYs,Y,R).
most_recent_year( [_|PYs] , T , R ) :- most_recent_year(PYs,T,R).
https://stackoverflow.com/questions/73808674
复制相似问题