首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过查询在Prolog中详尽地搜索知识库

通过查询在Prolog中详尽地搜索知识库
EN

Stack Overflow用户
提问于 2022-09-22 02:20:09
回答 2查看 43关注 0票数 1

如果我有一个人的知识库和他们的生日,比如:

代码语言:javascript
运行
复制
birthYear( adam , 2000 ).
birthYear( bob  , 2001 ).
birthYear( john , 2002 ).

如何创建一个查询,在不修改知识库的情况下,详尽地搜索知识库,找到最年轻的人?

EN

回答 2

Stack Overflow用户

发布于 2022-09-22 08:22:42

可以使用

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
?- youngest_person(Person).
Person = adam.
票数 0
EN

Stack Overflow用户

发布于 2022-09-22 18:50:45

像这样吗?

代码语言:javascript
运行
复制
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).
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73808674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档