Support
Quality
Security
License
Reuse
kandi has reviewed 99-problems and discovered the below as its top functions. This is intended to give you an instant insight into 99-problems implemented functionality, and help decide if they suit your requirements.
This is an adaptation of the Ninety-Nine Prolog Problems written by Werner Hett.
Prolog :Make my predicate return all possible solutions
choose(X, L1, L2) :-
append(A, [X|B], L1),
append(A, B, L2).
?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.
group234(G, [A,B], [C,D,E], G4):-
choose(A, G, G0),
choose(B, G0, G1), A @< B,
choose(C, G1, G2),
choose(D, G2, G3), C @< D,
choose(E, G3, G4), D @< E.
?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i]
...
-----------------------
choose(X, L1, L2) :-
append(A, [X|B], L1),
append(A, B, L2).
?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.
group234(G, [A,B], [C,D,E], G4):-
choose(A, G, G0),
choose(B, G0, G1), A @< B,
choose(C, G1, G2),
choose(D, G2, G3), C @< D,
choose(E, G3, G4), D @< E.
?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i]
...
-----------------------
choose(X, L1, L2) :-
append(A, [X|B], L1),
append(A, B, L2).
?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.
group234(G, [A,B], [C,D,E], G4):-
choose(A, G, G0),
choose(B, G0, G1), A @< B,
choose(C, G1, G2),
choose(D, G2, G3), C @< D,
choose(E, G3, G4), D @< E.
?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i]
...
-----------------------
choose(X, L1, L2) :-
append(A, [X|B], L1),
append(A, B, L2).
?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.
group234(G, [A,B], [C,D,E], G4):-
choose(A, G, G0),
choose(B, G0, G1), A @< B,
choose(C, G1, G2),
choose(D, G2, G3), C @< D,
choose(E, G3, G4), D @< E.
?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i]
...
-----------------------
%! %%%%
group234(G, [A,B], [C,D,E], G4):-
select(A, G, G0),
select(B, G0, G1), A @< B,
select(C, G1, G2),
select(D, G2, G3), C @< D,
select(E, G3, G4), D @< E.
n_group234_slago(N) :-
numlist(1,9,L),
aggregate_all(count,group234(L,_,_,_),N).
take_ordered(L,[X],R) :-
select(X,L,R).
take_ordered(L,[X|Xs],R) :-
append(H,[X|T],L),
take_ordered(T,Xs,J),
append(H,J,R).
group234_cc(L,[A1,A2],[B1,B2,B3],[C1,C2,C3,C4]) :-
take_ordered(L,[A1,A2],U),
take_ordered(U,[B1,B2,B3],[C1,C2,C3,C4]).
n_group234_cc(N) :-
numlist(1,9,L),
aggregate_all(count,group234_cc(L,_A,_B,_C),N).
take_ordered([],[]) --> [].
take_ordered([X|Xs],Ys) --> [X],
take_ordered(Xs,Ys).
take_ordered(Xs,[Y|Ys]) --> [Y],
take_ordered(Xs,Ys).
take_ordered(L,O,R) :-
phrase(take_ordered(O,R),L).
?- numlist(1,9,L),time(aggregate_all(count,group3(L,A,B,C),N)).
% 122,373 inferences, ...
?- time(n_group234_cc(N)).
% 7,549 inferences, ...
-----------------------
%! %%%%
group234(G, [A,B], [C,D,E], G4):-
select(A, G, G0),
select(B, G0, G1), A @< B,
select(C, G1, G2),
select(D, G2, G3), C @< D,
select(E, G3, G4), D @< E.
n_group234_slago(N) :-
numlist(1,9,L),
aggregate_all(count,group234(L,_,_,_),N).
take_ordered(L,[X],R) :-
select(X,L,R).
take_ordered(L,[X|Xs],R) :-
append(H,[X|T],L),
take_ordered(T,Xs,J),
append(H,J,R).
group234_cc(L,[A1,A2],[B1,B2,B3],[C1,C2,C3,C4]) :-
take_ordered(L,[A1,A2],U),
take_ordered(U,[B1,B2,B3],[C1,C2,C3,C4]).
n_group234_cc(N) :-
numlist(1,9,L),
aggregate_all(count,group234_cc(L,_A,_B,_C),N).
take_ordered([],[]) --> [].
take_ordered([X|Xs],Ys) --> [X],
take_ordered(Xs,Ys).
take_ordered(Xs,[Y|Ys]) --> [Y],
take_ordered(Xs,Ys).
take_ordered(L,O,R) :-
phrase(take_ordered(O,R),L).
?- numlist(1,9,L),time(aggregate_all(count,group3(L,A,B,C),N)).
% 122,373 inferences, ...
?- time(n_group234_cc(N)).
% 7,549 inferences, ...
-----------------------
%! %%%%
group234(G, [A,B], [C,D,E], G4):-
select(A, G, G0),
select(B, G0, G1), A @< B,
select(C, G1, G2),
select(D, G2, G3), C @< D,
select(E, G3, G4), D @< E.
n_group234_slago(N) :-
numlist(1,9,L),
aggregate_all(count,group234(L,_,_,_),N).
take_ordered(L,[X],R) :-
select(X,L,R).
take_ordered(L,[X|Xs],R) :-
append(H,[X|T],L),
take_ordered(T,Xs,J),
append(H,J,R).
group234_cc(L,[A1,A2],[B1,B2,B3],[C1,C2,C3,C4]) :-
take_ordered(L,[A1,A2],U),
take_ordered(U,[B1,B2,B3],[C1,C2,C3,C4]).
n_group234_cc(N) :-
numlist(1,9,L),
aggregate_all(count,group234_cc(L,_A,_B,_C),N).
take_ordered([],[]) --> [].
take_ordered([X|Xs],Ys) --> [X],
take_ordered(Xs,Ys).
take_ordered(Xs,[Y|Ys]) --> [Y],
take_ordered(Xs,Ys).
take_ordered(L,O,R) :-
phrase(take_ordered(O,R),L).
?- numlist(1,9,L),time(aggregate_all(count,group3(L,A,B,C),N)).
% 122,373 inferences, ...
?- time(n_group234_cc(N)).
% 7,549 inferences, ...
-----------------------
%! %%%%
group234(G, [A,B], [C,D,E], G4):-
select(A, G, G0),
select(B, G0, G1), A @< B,
select(C, G1, G2),
select(D, G2, G3), C @< D,
select(E, G3, G4), D @< E.
n_group234_slago(N) :-
numlist(1,9,L),
aggregate_all(count,group234(L,_,_,_),N).
take_ordered(L,[X],R) :-
select(X,L,R).
take_ordered(L,[X|Xs],R) :-
append(H,[X|T],L),
take_ordered(T,Xs,J),
append(H,J,R).
group234_cc(L,[A1,A2],[B1,B2,B3],[C1,C2,C3,C4]) :-
take_ordered(L,[A1,A2],U),
take_ordered(U,[B1,B2,B3],[C1,C2,C3,C4]).
n_group234_cc(N) :-
numlist(1,9,L),
aggregate_all(count,group234_cc(L,_A,_B,_C),N).
take_ordered([],[]) --> [].
take_ordered([X|Xs],Ys) --> [X],
take_ordered(Xs,Ys).
take_ordered(Xs,[Y|Ys]) --> [Y],
take_ordered(Xs,Ys).
take_ordered(L,O,R) :-
phrase(take_ordered(O,R),L).
?- numlist(1,9,L),time(aggregate_all(count,group3(L,A,B,C),N)).
% 122,373 inferences, ...
?- time(n_group234_cc(N)).
% 7,549 inferences, ...
QUESTION
Prolog :Make my predicate return all possible solutions
Asked 2021-Jun-19 at 14:44I found this problem in 99-problems in prolog online. There is a solution (has nothing to do with mine) and I was wondering why mine won't work. Or to be precise: it works but it finds only 1 solution instead of all of them. The problem is stated as such: a) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons?
member(X,[X]).
member(X,[X|_]).
member(X,[_|R]):- member(X,R).
append(X,[],X).
append([],X,X).
append([H|R], [A|B], [H|W]):- append(R,[A|B],W).
group234(G,G2,G3,G4):- length(G2,2),
length(G3,3),
length(G4,4),
member(X,G),
member(Y,G),
member(Z,G),
member(X,G2),
member(Y,G2),
member(Z,G3),
append(G2,G3,I),append(I,G4,G).
q1: Is there a way to use length and append and member as I did and sole this succesfully or do I need to completely rewrite this?
q2: Why does this code produce only 1 solution? Prolog should search for many possible members, shouldn't it? (Obviously , it should not , because the language knows better than I do . But to my understanding it should so why it does not?)
ANSWER
Answered 2021-Jun-19 at 01:41You need only append/3
to define a predicate to choose and remove one person of a group:
choose(X, L1, L2) :-
append(A, [X|B], L1),
append(A, B, L2).
For example:
?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.
Then, using this predicate, you can define group234/4
as:
group234(G, [A,B], [C,D,E], G4):-
choose(A, G, G0),
choose(B, G0, G1), A @< B,
choose(C, G1, G2),
choose(D, G2, G3), C @< D,
choose(E, G3, G4), D @< E.
Notice that you need condition A @< B
, to avoid permutations (since both lists [A,B]
and [B,A]
represent the same group). Analogously, conditions C @< D
and D @< E
avoid permutations of the list [C,D,E]
.
Example:
?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i]
...
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit