如果我有一个关于人和他们生日的知识库,比如:
birthYear( adam , 2000 ).
birthYear( bob , 2001 ).
birthYear( john , 2002 ).
在不修改知识库的情况下,我如何创建一个查询来彻底搜索知识库以找到最年轻的人?
可以使用aggregate_all:
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.
类似的东西?
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).