Prolog powerset predicate [closed]

牧云@^-^@ 提交于 2020-01-02 19:32:42

问题


I wish to define a predicate powerset(X, P) which is true when P is the powerset of X. Should work whether or not P is ground.


回答1:


Since you use SICStus Prolog you can use the subseq0(+Sequence, ?SubSequence) from library(lists), which "is true when SubSequence is a subsequence of Sequence, but may be Sequence itself" (Quoting from the manual http://www.sics.se/sicstus/docs/4.0.2/html/sicstus/lib_002dlists.html).

      ?- setof(X, subseq0([a,b,c],X), Xs).
      Xs = [[],[a],[a,b],[a,b,c],[a,c],[b],[b,c],[c]]

If you are not allowed to use library predicates you can implement the subseteq0 as explained in gnu Prolog powerset modification, which I quote here for the sake of completeness (with thanks to gusbro)

powerset([], []).
powerset([H|T], P) :- powerset(T,P).
powerset([H|T], [H|P]) :- powerset(T,P).


来源:https://stackoverflow.com/questions/10338666/prolog-powerset-predicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!