【问题标题】:How to get a list with all the possible combinations of the elements inside another list?如何获得一个列表,其中包含另一个列表中元素的所有可能组合?
【发布时间】:2015-10-24 19:06:56
【问题描述】:

我有一个包含元素的列表:原子和其他列表。我问以下问题:

-? totobola([1,x,2,1,x,2,1,x,2,1,x,[1,x],[1,x,2]],LS)。


我希望 LS 成为一个列表,我将在其中显示每个原子,然后是给定列表中列表的可能组合。

LS=[1,x,2,1,x,2,1,x,2,1,x,1,1];
LS=[1,x,2,1,x,2,1,x,2,1,x,1,x];
LS=[1,x,2,1,x,2,1,x,2,1,x,1,2];
LS=[1,x,2,1,x,2,1,x,2,1,x,x,1];
LS=[1,x,2,1,x,2,1,x,2,1,x,x,x];
LS=[1,x,2,1,x,2,1,x,2,1,x,x,2];
没有

我目前的解决方案:

lista([_|_]):- true, !.
lista(_):- false.

totobola([],[]).
totobola([X|T1],[Y|T2]):-
    lista(X),
    !,
    member(Y,X),
    totobola(T1,T2).
totobola([X|T1],[X|T2]):-
    totobola(T1,T2).

使用lista,我检查X 是否是一个列表,但不是得到LS=[1,x,2,1,x,2,1,x,2,1,x,1,1];,而是得到LS = [1, x, 2, 1, x, 2, 1, x, 2|...]

谁能指导我或至少告诉我我做错了什么或不太正确?

提前致谢!

【问题讨论】:

  • 确保您查看的是whole list。而且,从技术上讲,您应该拥有lista([]).lista([_|_]).(而不是lista([_|_]) :- true, !),但我认为这不会影响您当前的解决方案。您可以省略 lista(_) :- false.,因为任何未通过规则或事实声明为 true 的内容默认情况下都会失败。
  • 请提供更多(和更小的)示例!另外,解释一下原子和列表代表什么......
  • @lurker 你是对的 lista 我可以并且会省略错误条款。我会确保我正在查看整个列表并回复您。 @repeat 例如:[1,x,2,1,x, [x,1],[1,x,2]] 在这种情况下,我指的是列表中的单个元素:1,x,2,1,x 并列出这些括号内的所有内容 [x,1][1,x,2]
  • 请解释12x[1,x,2][x,1]是什么意思意思1 代表什么(2 不代表)? [x,1] 代表什么? [1,x], [1,x,x], [x,x]...

标签: list prolog


【解决方案1】:

做一个“重复”。查询然后按'w'键和'.'您将进入写入模式并查看列表中的所有值。

?- 重复。
true [write] --> 按 w 一次后
真的 。 --> 输入一个点退出

【讨论】:

    【解决方案2】:

    我们在 ALGAV 课上进行了这个练习,ISEP (Portugal),解决方案是:

    is_list(X) :- var(X), !, fail.
    is_list([]).
    is_list([_|T]) :- is_list(T).
    
    totobola([],[]).
    totobola([H|T],[X|LR]):- is_list(H), !, member(X,H), totobola(T,LR). 
    totobola([H|T],[H|LR]):- totobola(T,LR).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2020-01-14
      • 2010-10-02
      • 2020-02-28
      相关资源
      最近更新 更多