Prolog powerset predicate [closed]

风流意气都作罢 提交于 2019-12-06 09:29:48
Alexander Serebrenik

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