0

I am currently struggling with Prolog for a project. I am trying to write a method that basically creates a group on subgroups.

Basically I want the following result on my query:

`?- 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]].`

Instead only the second sublist actually gets shuffled. The first sublist is always the same.:

`?- 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]] ;`

Does anyone have any idea how I can fix that?

My goal is to use make_sublists to create the sublists out of the given the lists. Then I want to connect these sublists to one List.

Any help would be appreciated! Sorry for my writing, English isn't my first language.

My code looks like this:

% 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).
Parniä
  • 1
  • 1
  • Use `select`, as in e.g. https://stackoverflow.com/a/75636324/ , and avoid `append` - this is Prolog not Python :-) – brebs Mar 29 '23 at 06:22

0 Answers0