Prolog - how to do setof that returns empty list instead of failing

后端 未结 3 749
臣服心动
臣服心动 2020-12-20 03:49

I need an ordered list of Objects that satisfy Goal. setof takes care of the ordering, but fails when no Objects satisfy Goal. I want to return an empty list in

3条回答
  •  没有蜡笔的小新
    2020-12-20 03:50

    If you do not need the potential nondeterminism or the variable-quantification features of setof, you can stick with findall/3. This is deterministic and doesn't fail:

    ?- findall(X, fail, Xs).
    Xs = []
    yes
    

    You can then sort the results yourself using sort/2:

    findall(Object, Goal, UnsortedWithDuplicates),
    sort(UnsortedWithDuplicates, List)
    

提交回复
热议问题