gnu Prolog powerset 修改 [英] gnu Prolog powerset modification

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

问题描述

所以我得到了这个用于 powerset:

So i got this for powerset:

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

这会生成一个列表的所有集合.是否可以按列表顺序生成所有集合.

This generates all sets of a list. Is it possible to generate all sets in list order.

例子:

List = [a,b,c]

我想得到

[a],[a,b],[a,b,c],[b],[b,c],[c]

请注意,此子集列表中没有 [a,c],因为这些子集是从左到右的子集.

Note there is no [a,c] in this list of subsets since these are subsets starting from the left and going to the right.

我尝试过使用追加和递归的组合,但并没有达到我想要的效果.在这一点上有点难过.

I've tried using a combination of append and recursion, but that didn't work out as i wanted it to. Little stumped at this point.

谢谢.

推荐答案

怎么样

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

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

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