Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select unique elements in Prolog

Tags:

prolog

Given facts like

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

how to get a unique result [a,b]?


1 Answers

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.

The construct +Var^Goal tells setof/3 not to bind Var in Goal.

?- setof(X, Y^foo(Y, X), Result).
Result = [a, b].
like image 83
fferri Avatar answered Mar 04 '26 17:03

fferri