Nice to know ... setof/3 and ^/0

An easy way to extract an ordered list from predicates:

myrec(1,a).
myrec(1,b).
myrec(2,b).
myrec(3,c).
myrec(2,c).

?-  setof(Record, Data^myrec(Record,Data), Results).
Results = [1, 2, 3]

To read as: ““Find the set for all myrec, such that the record has some Data, and put the result in Results (ordered and with no duplicate).”

2 Likes