简体   繁体   English

在 Prolog 中选择独特的元素

[英]Select unique elements in Prolog

Given facts like鉴于事实如

foo(1,a).
foo(2,a).
foo(3,b).

how to get a unique result [a,b] ?如何获得唯一的结果[a,b]

setof/3 : The built-in Prolog predicate setof(+Template, +Goal, -Set) binds Set to the list of all instances of Template satisfying the goal Goal . setof/3 :内置的 Prolog 谓词setof(+Template, +Goal, -Set)Set绑定到满足目标GoalTemplate的所有实例的列表。

The construct +Var^Goal tells setof/3 not to bind Var in Goal .构造+Var^Goal告诉setof/3不要在Goal绑定Var

?- setof(X, Y^foo(Y, X), Result).
Result = [a, b].

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM