你好我在 prolog 上有一个巨大的数据库,我正在尝试编写一个比较两个名字及其年龄的规则。例如我的数据库是:年龄(A,12)年龄(B,56)年龄(C,80)...
我想写一个规则,用户将输入名称 A 和名称 B(A,B),程序确定 a 是否大于 b。
0
我会用这个简单的方法:
age(bill,12).
age(bob,56).
age(betty,80).
older(NAME_A,NAME_B) :-
age(NAME_A,AGE_A),
age(NAME_B,AGE_B),
AGE_A > AGE_B.
/*
?- older(bill,bob).
false.
?- older(bob,bill).
true.
*/
-1
age(bill,12) .
age(bob,56) .
age(betty,80) .
older(NAME_A,NAME_B,OLDER)
:-
age(NAME_A,AGE_A) ,
age(NAME_B,AGE_B) ,
older_compare(AGE_A,AGE_B,OLDER)
.
older_compare(AGE_A,AGE_B,true)
:-
AGE_A > AGE_B ,
!
.
older_compare(AGE_A,AGE_B,false)
.
/*
?- older(bill,bob,OLDER) .
OLDER = false .
?- older(bob,bill,OLDER) .
OLDER = true .
?- older(bill,bob,OLDER) .
OLDER = false .
?- older(betty,bob,OLDER) .
OLDER = true .
?- older(bob,betty,OLDER) .
OLDER = false .
?- older(betty,bill,OLDER) .
OLDER = true .
?- older(bill,betty,OLDER) .
OLDER = false .
*/
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(57条)