Prolog Powerset谓词 [英] Prolog powerset predicate

查看:110
本文介绍了Prolog Powerset谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望定义一个谓词powerset(X,P),当P是X的幂集时为true.无论P是否为接地,都应起作用.

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.

推荐答案

由于您使用SICStus Prolog,因此可以使用库(列表)中的subseq0(+ Sequence,?SubSequence),当SubSequence是以下子序列时,这是正确的"序列,但也可以是序列本身"(引自手册 http://www.sics.se/sicstus/docs/4.0.2/html/sicstus/lib_002dlists.html ).

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]]

如果不允许使用库谓词,则可以按照 gnu Prolog powerset修改中的说明实现subseteq0. ,出于完整性考虑,在此引用(感谢 gusbro )

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).

这篇关于Prolog Powerset谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆