我怎样才能确保我组的第一个子组在序言中被洗牌

问题描述 投票:0回答:0

我目前正在为一个项目纠结于Prolog。我正在尝试编写一种基本上在子组上创建组的方法。

基本上我想在我的查询中得到以下结果:

`?- make_list(List, [a, b, c, d, e, f], 3).

List = [[a, b, c], [d, e, f]] ;
List = [[a, d, e], [b, c, f]] ;
List = [[a, d, f], [b, c, e]] ;
List = [[a, e, f], [b, c, d]] ;
List = [[b, d, e], [a, c, f]] ;
List = [[b, d, f], [a, c, e]] ;
List = [[b, e, f], [a, c, d]] ;
List = [[c, d, e], [a, b, f]] ;
List = [[c, d, f], [a, b, e]] ;
List = [[c, e, f], [a, b, d]].`

实际上只有第二个子列表被洗牌。第一个子列表总是相同的。:

`?- make_list(List, [a, b, c, d, e, f], 3).
List = [[a, b, c], [d, e, f]] ;
List = [[a, b, c], [e, d, f]] ;
List = [[a, b, c], [e, f, d]] ;
List = [[a, b, c], [d, f, e]] ;
List = [[a, b, c], [f, d, e]] ;
List = [[a, b, c], [f, e, d]] ;`

有人知道我该如何解决吗?

我的目标是使用 make_sublists 从给定的列表中创建子列表。然后我想将这些子列表连接到一个列表。

任何帮助将不胜感激!对不起我的写作,英语不是我的第一语言。

我的代码是这样的:

% shuffle/2
shuffle([],[]).           
shuffle([A|As],Bs) :-     
  shuffle(As,Xs),         
  append(Ps,Qs,Xs),     
  append(Ps,[A|Qs],Bs).  

%make_one_sublist/4
%make_one_sublist(?List, +List_of_Elements, ?Elements_rest, +List_size)
make_one_sublist(List, List_of_Elements, Elements_rest, List_size) :-
    length(List, List_size),
    shuffle(List_of_Elements, ShuffledList), 
    append(List, Elements_rest, ShuffledList), 
    check_sublists([List], Elements_rest).    %check_sublists is another method/ predicate to check for all subgroups of Group1 that at most one element occurs in Group2

% make_list/3
% make_list(?Lists, +List_of_Elements, +List_size)
make_list([], [], _).
make_list(Lists, List_of_Elements, List_size) :-
    length(List_of_Elements, Length),
    Length >= List_size,
    make_one_list(List, List_of_Elements, Rest, List_size),
    make_list(RestLists, Rest, List_size),
    append([List], RestLists, Lists).
list prolog logic
© www.soinside.com 2019 - 2024. All rights reserved.