序言比

在 Prolog 中找到谁最高的最简单方法是什么:

height(lisa,1.65).
height(sam,1.70).
height(luke,1.92).
height(nicole,1.54).

我想写

tallest(X) :- Y is bigger than other Y's

回答

SWI-Prolog 有一些不同的方法来解决这个问题,例如通过库(solution_sequences)

?- order_by([desc(H)],height(P,H)).
H = 1.92,
P = luke ;
...

或使用库(聚合):

?- aggregate(max(H,P),height(P,H),max(_,P)).
P = luke.

不太复杂的 Prologs 可能会提供 setof/3 和 last/2:

?- setof(H:P,height(P,H),L),last(L,_:P).
P = luke,
L = [1.54:nicole, 1.65:lisa, 1.7:sam, 1.92:luke].

还有更多基本引擎,缺少 setof/3,将提供

?- height(P,H),+((height(_,L),L>H)).
P = luke,
H = 1.92 ;


以上是序言比的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>