How to obtain ALL the combinations with repetition of the elements of a list - prolog
I'm trying to get all the combinations with repetition of all the elements in a list L . These elements need to be returned in a size N list. (L, N, CWR).
The expected result would be something like:
?-([red,blue,green], 2 , X).
X = [red, red] ;
X = [red, blue] ;
X = [red, green] ;
X = [blue, blue] ;
X = [blue, green] ;
X = [blue, red] ;
X = [green, green] ;
X = [green, blue] ;
X = [green, red] ;
false.
prolog
add a comment |
I'm trying to get all the combinations with repetition of all the elements in a list L . These elements need to be returned in a size N list. (L, N, CWR).
The expected result would be something like:
?-([red,blue,green], 2 , X).
X = [red, red] ;
X = [red, blue] ;
X = [red, green] ;
X = [blue, blue] ;
X = [blue, green] ;
X = [blue, red] ;
X = [green, green] ;
X = [green, blue] ;
X = [green, red] ;
false.
prolog
1
What have you tried? This code obviously won’t work, also because in Prolog, only variables are capitalized and atoms must be lowercase or quoted appropriately. Have you usedmember/2? It’s also often useful to uselength/2to make a list of a prescribed length without specifying its contents.
– Daniel Lyons
Dec 30 '18 at 7:06
add a comment |
I'm trying to get all the combinations with repetition of all the elements in a list L . These elements need to be returned in a size N list. (L, N, CWR).
The expected result would be something like:
?-([red,blue,green], 2 , X).
X = [red, red] ;
X = [red, blue] ;
X = [red, green] ;
X = [blue, blue] ;
X = [blue, green] ;
X = [blue, red] ;
X = [green, green] ;
X = [green, blue] ;
X = [green, red] ;
false.
prolog
I'm trying to get all the combinations with repetition of all the elements in a list L . These elements need to be returned in a size N list. (L, N, CWR).
The expected result would be something like:
?-([red,blue,green], 2 , X).
X = [red, red] ;
X = [red, blue] ;
X = [red, green] ;
X = [blue, blue] ;
X = [blue, green] ;
X = [blue, red] ;
X = [green, green] ;
X = [green, blue] ;
X = [green, red] ;
false.
prolog
prolog
edited Dec 30 '18 at 18:08
false
10.4k771144
10.4k771144
asked Dec 30 '18 at 3:57
Zé SerraZé Serra
11
11
1
What have you tried? This code obviously won’t work, also because in Prolog, only variables are capitalized and atoms must be lowercase or quoted appropriately. Have you usedmember/2? It’s also often useful to uselength/2to make a list of a prescribed length without specifying its contents.
– Daniel Lyons
Dec 30 '18 at 7:06
add a comment |
1
What have you tried? This code obviously won’t work, also because in Prolog, only variables are capitalized and atoms must be lowercase or quoted appropriately. Have you usedmember/2? It’s also often useful to uselength/2to make a list of a prescribed length without specifying its contents.
– Daniel Lyons
Dec 30 '18 at 7:06
1
1
What have you tried? This code obviously won’t work, also because in Prolog, only variables are capitalized and atoms must be lowercase or quoted appropriately. Have you used
member/2? It’s also often useful to use length/2 to make a list of a prescribed length without specifying its contents.– Daniel Lyons
Dec 30 '18 at 7:06
What have you tried? This code obviously won’t work, also because in Prolog, only variables are capitalized and atoms must be lowercase or quoted appropriately. Have you used
member/2? It’s also often useful to use length/2 to make a list of a prescribed length without specifying its contents.– Daniel Lyons
Dec 30 '18 at 7:06
add a comment |
3 Answers
3
active
oldest
votes
Taking a higher-level view:
combinations_( L, N, R) :-
length( R, N),
maplist( flip(member,L), R).
flip( P, L, X):- call(P, X, L).
We just create a list of length N and fill it up with all the elements of L one after another.
I first saw flip on RosettaCode's Zebra Puzzle page.
Your answer had a nice insight I didn't see.
– Guy Coder
Dec 30 '18 at 13:49
You should add a working example to the answer. When I first saw this I had to test it to verify it worked when N was 1 or greater than 2 and worked with empty list and list of just one item.
– Guy Coder
Dec 30 '18 at 13:53
@GuyCoder Somehow it feels very natural and self-evident, to me. :)
– Will Ness
Dec 31 '18 at 7:03
add a comment |
This works in SWI-Prolog:
all_combos(_, 0, ).
all_combos(L, N, [H|T]) :-
length([H|T], N),
N1 is N - 1,
member(H, L),
all_combos(L, N1, T).
While this may answer the question, it is better to include the essential parts of the answer here and provide the context for reference. code-only answers can become invalid if there's no explanation.
– Viira
Jan 3 at 4:48
add a comment |
This is a good start :
?- [user] .
% consulting user_input...
:- op(2'1,'yfx','of') .
(
_yO_ of _Xs_
)
:-
(
(
= _Xs_ ;
)
;
(
[_yO_|_xS_] = _Xs_
)
;
(
[_|_xS_] = _Xs_ ,
_yO_ of _xS_
)
)
.
%^D%
% consulting user_query...
?-
_Xs_ = ['red','green','blue'] ,
_pO_ of _Xs_ ,
_qO_ of _Xs_ ,
Ys = [_pO_,_qO_] .
Ys = [red,red] ? ;
Ys = [red,green] ? ;
Ys = [red,blue] ? ;
Ys = [red,_qO_] ? ;
Ys = [green,red] ? ;
Ys = [green,green] ? ;
Ys = [green,blue] ? ;
Ys = [green,_qO_] ? ;
Ys = [blue,red] ? ;
Ys = [blue,green] ? ;
Ys = [blue,blue] ? ;
Ys = [blue,_qO_] ? ;
Ys = [_pO_,red] ? ;
Ys = [_pO_,green] ? ;
Ys = [_pO_,blue] ? ;
Ys = [_pO_,_qO_] ? ;
false
?-
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975167%2fhow-to-obtain-all-the-combinations-with-repetition-of-the-elements-of-a-list-p%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Taking a higher-level view:
combinations_( L, N, R) :-
length( R, N),
maplist( flip(member,L), R).
flip( P, L, X):- call(P, X, L).
We just create a list of length N and fill it up with all the elements of L one after another.
I first saw flip on RosettaCode's Zebra Puzzle page.
Your answer had a nice insight I didn't see.
– Guy Coder
Dec 30 '18 at 13:49
You should add a working example to the answer. When I first saw this I had to test it to verify it worked when N was 1 or greater than 2 and worked with empty list and list of just one item.
– Guy Coder
Dec 30 '18 at 13:53
@GuyCoder Somehow it feels very natural and self-evident, to me. :)
– Will Ness
Dec 31 '18 at 7:03
add a comment |
Taking a higher-level view:
combinations_( L, N, R) :-
length( R, N),
maplist( flip(member,L), R).
flip( P, L, X):- call(P, X, L).
We just create a list of length N and fill it up with all the elements of L one after another.
I first saw flip on RosettaCode's Zebra Puzzle page.
Your answer had a nice insight I didn't see.
– Guy Coder
Dec 30 '18 at 13:49
You should add a working example to the answer. When I first saw this I had to test it to verify it worked when N was 1 or greater than 2 and worked with empty list and list of just one item.
– Guy Coder
Dec 30 '18 at 13:53
@GuyCoder Somehow it feels very natural and self-evident, to me. :)
– Will Ness
Dec 31 '18 at 7:03
add a comment |
Taking a higher-level view:
combinations_( L, N, R) :-
length( R, N),
maplist( flip(member,L), R).
flip( P, L, X):- call(P, X, L).
We just create a list of length N and fill it up with all the elements of L one after another.
I first saw flip on RosettaCode's Zebra Puzzle page.
Taking a higher-level view:
combinations_( L, N, R) :-
length( R, N),
maplist( flip(member,L), R).
flip( P, L, X):- call(P, X, L).
We just create a list of length N and fill it up with all the elements of L one after another.
I first saw flip on RosettaCode's Zebra Puzzle page.
answered Dec 30 '18 at 13:33
Will NessWill Ness
44.4k468122
44.4k468122
Your answer had a nice insight I didn't see.
– Guy Coder
Dec 30 '18 at 13:49
You should add a working example to the answer. When I first saw this I had to test it to verify it worked when N was 1 or greater than 2 and worked with empty list and list of just one item.
– Guy Coder
Dec 30 '18 at 13:53
@GuyCoder Somehow it feels very natural and self-evident, to me. :)
– Will Ness
Dec 31 '18 at 7:03
add a comment |
Your answer had a nice insight I didn't see.
– Guy Coder
Dec 30 '18 at 13:49
You should add a working example to the answer. When I first saw this I had to test it to verify it worked when N was 1 or greater than 2 and worked with empty list and list of just one item.
– Guy Coder
Dec 30 '18 at 13:53
@GuyCoder Somehow it feels very natural and self-evident, to me. :)
– Will Ness
Dec 31 '18 at 7:03
Your answer had a nice insight I didn't see.
– Guy Coder
Dec 30 '18 at 13:49
Your answer had a nice insight I didn't see.
– Guy Coder
Dec 30 '18 at 13:49
You should add a working example to the answer. When I first saw this I had to test it to verify it worked when N was 1 or greater than 2 and worked with empty list and list of just one item.
– Guy Coder
Dec 30 '18 at 13:53
You should add a working example to the answer. When I first saw this I had to test it to verify it worked when N was 1 or greater than 2 and worked with empty list and list of just one item.
– Guy Coder
Dec 30 '18 at 13:53
@GuyCoder Somehow it feels very natural and self-evident, to me. :)
– Will Ness
Dec 31 '18 at 7:03
@GuyCoder Somehow it feels very natural and self-evident, to me. :)
– Will Ness
Dec 31 '18 at 7:03
add a comment |
This works in SWI-Prolog:
all_combos(_, 0, ).
all_combos(L, N, [H|T]) :-
length([H|T], N),
N1 is N - 1,
member(H, L),
all_combos(L, N1, T).
While this may answer the question, it is better to include the essential parts of the answer here and provide the context for reference. code-only answers can become invalid if there's no explanation.
– Viira
Jan 3 at 4:48
add a comment |
This works in SWI-Prolog:
all_combos(_, 0, ).
all_combos(L, N, [H|T]) :-
length([H|T], N),
N1 is N - 1,
member(H, L),
all_combos(L, N1, T).
While this may answer the question, it is better to include the essential parts of the answer here and provide the context for reference. code-only answers can become invalid if there's no explanation.
– Viira
Jan 3 at 4:48
add a comment |
This works in SWI-Prolog:
all_combos(_, 0, ).
all_combos(L, N, [H|T]) :-
length([H|T], N),
N1 is N - 1,
member(H, L),
all_combos(L, N1, T).
This works in SWI-Prolog:
all_combos(_, 0, ).
all_combos(L, N, [H|T]) :-
length([H|T], N),
N1 is N - 1,
member(H, L),
all_combos(L, N1, T).
edited Jan 3 at 5:55
answered Jan 3 at 2:51
Valera GrishinValera Grishin
33126
33126
While this may answer the question, it is better to include the essential parts of the answer here and provide the context for reference. code-only answers can become invalid if there's no explanation.
– Viira
Jan 3 at 4:48
add a comment |
While this may answer the question, it is better to include the essential parts of the answer here and provide the context for reference. code-only answers can become invalid if there's no explanation.
– Viira
Jan 3 at 4:48
While this may answer the question, it is better to include the essential parts of the answer here and provide the context for reference. code-only answers can become invalid if there's no explanation.
– Viira
Jan 3 at 4:48
While this may answer the question, it is better to include the essential parts of the answer here and provide the context for reference. code-only answers can become invalid if there's no explanation.
– Viira
Jan 3 at 4:48
add a comment |
This is a good start :
?- [user] .
% consulting user_input...
:- op(2'1,'yfx','of') .
(
_yO_ of _Xs_
)
:-
(
(
= _Xs_ ;
)
;
(
[_yO_|_xS_] = _Xs_
)
;
(
[_|_xS_] = _Xs_ ,
_yO_ of _xS_
)
)
.
%^D%
% consulting user_query...
?-
_Xs_ = ['red','green','blue'] ,
_pO_ of _Xs_ ,
_qO_ of _Xs_ ,
Ys = [_pO_,_qO_] .
Ys = [red,red] ? ;
Ys = [red,green] ? ;
Ys = [red,blue] ? ;
Ys = [red,_qO_] ? ;
Ys = [green,red] ? ;
Ys = [green,green] ? ;
Ys = [green,blue] ? ;
Ys = [green,_qO_] ? ;
Ys = [blue,red] ? ;
Ys = [blue,green] ? ;
Ys = [blue,blue] ? ;
Ys = [blue,_qO_] ? ;
Ys = [_pO_,red] ? ;
Ys = [_pO_,green] ? ;
Ys = [_pO_,blue] ? ;
Ys = [_pO_,_qO_] ? ;
false
?-
add a comment |
This is a good start :
?- [user] .
% consulting user_input...
:- op(2'1,'yfx','of') .
(
_yO_ of _Xs_
)
:-
(
(
= _Xs_ ;
)
;
(
[_yO_|_xS_] = _Xs_
)
;
(
[_|_xS_] = _Xs_ ,
_yO_ of _xS_
)
)
.
%^D%
% consulting user_query...
?-
_Xs_ = ['red','green','blue'] ,
_pO_ of _Xs_ ,
_qO_ of _Xs_ ,
Ys = [_pO_,_qO_] .
Ys = [red,red] ? ;
Ys = [red,green] ? ;
Ys = [red,blue] ? ;
Ys = [red,_qO_] ? ;
Ys = [green,red] ? ;
Ys = [green,green] ? ;
Ys = [green,blue] ? ;
Ys = [green,_qO_] ? ;
Ys = [blue,red] ? ;
Ys = [blue,green] ? ;
Ys = [blue,blue] ? ;
Ys = [blue,_qO_] ? ;
Ys = [_pO_,red] ? ;
Ys = [_pO_,green] ? ;
Ys = [_pO_,blue] ? ;
Ys = [_pO_,_qO_] ? ;
false
?-
add a comment |
This is a good start :
?- [user] .
% consulting user_input...
:- op(2'1,'yfx','of') .
(
_yO_ of _Xs_
)
:-
(
(
= _Xs_ ;
)
;
(
[_yO_|_xS_] = _Xs_
)
;
(
[_|_xS_] = _Xs_ ,
_yO_ of _xS_
)
)
.
%^D%
% consulting user_query...
?-
_Xs_ = ['red','green','blue'] ,
_pO_ of _Xs_ ,
_qO_ of _Xs_ ,
Ys = [_pO_,_qO_] .
Ys = [red,red] ? ;
Ys = [red,green] ? ;
Ys = [red,blue] ? ;
Ys = [red,_qO_] ? ;
Ys = [green,red] ? ;
Ys = [green,green] ? ;
Ys = [green,blue] ? ;
Ys = [green,_qO_] ? ;
Ys = [blue,red] ? ;
Ys = [blue,green] ? ;
Ys = [blue,blue] ? ;
Ys = [blue,_qO_] ? ;
Ys = [_pO_,red] ? ;
Ys = [_pO_,green] ? ;
Ys = [_pO_,blue] ? ;
Ys = [_pO_,_qO_] ? ;
false
?-
This is a good start :
?- [user] .
% consulting user_input...
:- op(2'1,'yfx','of') .
(
_yO_ of _Xs_
)
:-
(
(
= _Xs_ ;
)
;
(
[_yO_|_xS_] = _Xs_
)
;
(
[_|_xS_] = _Xs_ ,
_yO_ of _xS_
)
)
.
%^D%
% consulting user_query...
?-
_Xs_ = ['red','green','blue'] ,
_pO_ of _Xs_ ,
_qO_ of _Xs_ ,
Ys = [_pO_,_qO_] .
Ys = [red,red] ? ;
Ys = [red,green] ? ;
Ys = [red,blue] ? ;
Ys = [red,_qO_] ? ;
Ys = [green,red] ? ;
Ys = [green,green] ? ;
Ys = [green,blue] ? ;
Ys = [green,_qO_] ? ;
Ys = [blue,red] ? ;
Ys = [blue,green] ? ;
Ys = [blue,blue] ? ;
Ys = [blue,_qO_] ? ;
Ys = [_pO_,red] ? ;
Ys = [_pO_,green] ? ;
Ys = [_pO_,blue] ? ;
Ys = [_pO_,_qO_] ? ;
false
?-
answered Dec 30 '18 at 12:43
KintalkenKintalken
1408
1408
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975167%2fhow-to-obtain-all-the-combinations-with-repetition-of-the-elements-of-a-list-p%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What have you tried? This code obviously won’t work, also because in Prolog, only variables are capitalized and atoms must be lowercase or quoted appropriately. Have you used
member/2? It’s also often useful to uselength/2to make a list of a prescribed length without specifying its contents.– Daniel Lyons
Dec 30 '18 at 7:06