Getting all combinations of elements in a list (Prolog)
I'm trying to practice with list in Prolog.
In fact what I try to do is:
Get all combinations of elements in a list,
determine if the resulting list is longer than the input list. Here is the code:
%
descartes(,,false).
descartes(X,Result,IsExist):-
i_think(X,Result),
i_exists(X,Result,IsExist).
%should concatenate all combinations of input list
i_think(,NULL).
i_think([X|Xs],S):-
Y = Xs,
Y1 = X,
concatenate([X,Y1],Z1),
i_think(Y,T),
i_think(Xs,U),
S = [Z1,T,U].
%
concatenate([X,Y],R):-
atomics_to_string([X,Y],T),
atomics_to_string([Y,X],U),
R= [T,U].
%
i_exists(X,Y,false):-
len(X,Y,2);
len(X,Y,1).
i_exists(X,Y,true):-
len(X,Y,0).
%
len(X,Y,Count) :-
length(X,K),
length(Y,I),
(
(
K < I,
Count is 0
);
(
I < K,
Count is 2
);
(
I = K,
Count is 1
)
).
The main problem is with i_think/2, thanks for any help.
....
Specify I/O, of course, and thanks for replying:
descartes/3 :
descartes(['gold','bank'],Result,isExist)
Result=['goldgold','goldbank','bankbank','bankgold']
isExist= true
(In fact I would prefer to erase repetitions as...)
descartes(['gold','gold'],Result,isExist)
Result=['goldgold','goldgold','goldgold','goldgold']
(this shouldn't be generated as i_exist would return true)
isExist= false
prolog
add a comment |
I'm trying to practice with list in Prolog.
In fact what I try to do is:
Get all combinations of elements in a list,
determine if the resulting list is longer than the input list. Here is the code:
%
descartes(,,false).
descartes(X,Result,IsExist):-
i_think(X,Result),
i_exists(X,Result,IsExist).
%should concatenate all combinations of input list
i_think(,NULL).
i_think([X|Xs],S):-
Y = Xs,
Y1 = X,
concatenate([X,Y1],Z1),
i_think(Y,T),
i_think(Xs,U),
S = [Z1,T,U].
%
concatenate([X,Y],R):-
atomics_to_string([X,Y],T),
atomics_to_string([Y,X],U),
R= [T,U].
%
i_exists(X,Y,false):-
len(X,Y,2);
len(X,Y,1).
i_exists(X,Y,true):-
len(X,Y,0).
%
len(X,Y,Count) :-
length(X,K),
length(Y,I),
(
(
K < I,
Count is 0
);
(
I < K,
Count is 2
);
(
I = K,
Count is 1
)
).
The main problem is with i_think/2, thanks for any help.
....
Specify I/O, of course, and thanks for replying:
descartes/3 :
descartes(['gold','bank'],Result,isExist)
Result=['goldgold','goldbank','bankbank','bankgold']
isExist= true
(In fact I would prefer to erase repetitions as...)
descartes(['gold','gold'],Result,isExist)
Result=['goldgold','goldgold','goldgold','goldgold']
(this shouldn't be generated as i_exist would return true)
isExist= false
prolog
I ran this and found?- descartes([a,b],L,R). L = [["aa", "aa"], [["bb", "bb"], _6960, _6966], [["bb", "bb"], _7038, _7044]], R = true ;
. Can you give us a few sample input and expected output so we know what is suppose to happen. I ran your code and while I do see some combinations being generated, I see lots of extra undefined variables being created and have no idea what they are for. When you give the examples please do them for all of the predicates you created,descartes/2
,i_think/2
,i_exists/3
– Guy Coder
Nov 16 '18 at 10:47
FYI,NULL
is a variable since it begins with a capital letter.
– lurker
Nov 16 '18 at 14:36
add a comment |
I'm trying to practice with list in Prolog.
In fact what I try to do is:
Get all combinations of elements in a list,
determine if the resulting list is longer than the input list. Here is the code:
%
descartes(,,false).
descartes(X,Result,IsExist):-
i_think(X,Result),
i_exists(X,Result,IsExist).
%should concatenate all combinations of input list
i_think(,NULL).
i_think([X|Xs],S):-
Y = Xs,
Y1 = X,
concatenate([X,Y1],Z1),
i_think(Y,T),
i_think(Xs,U),
S = [Z1,T,U].
%
concatenate([X,Y],R):-
atomics_to_string([X,Y],T),
atomics_to_string([Y,X],U),
R= [T,U].
%
i_exists(X,Y,false):-
len(X,Y,2);
len(X,Y,1).
i_exists(X,Y,true):-
len(X,Y,0).
%
len(X,Y,Count) :-
length(X,K),
length(Y,I),
(
(
K < I,
Count is 0
);
(
I < K,
Count is 2
);
(
I = K,
Count is 1
)
).
The main problem is with i_think/2, thanks for any help.
....
Specify I/O, of course, and thanks for replying:
descartes/3 :
descartes(['gold','bank'],Result,isExist)
Result=['goldgold','goldbank','bankbank','bankgold']
isExist= true
(In fact I would prefer to erase repetitions as...)
descartes(['gold','gold'],Result,isExist)
Result=['goldgold','goldgold','goldgold','goldgold']
(this shouldn't be generated as i_exist would return true)
isExist= false
prolog
I'm trying to practice with list in Prolog.
In fact what I try to do is:
Get all combinations of elements in a list,
determine if the resulting list is longer than the input list. Here is the code:
%
descartes(,,false).
descartes(X,Result,IsExist):-
i_think(X,Result),
i_exists(X,Result,IsExist).
%should concatenate all combinations of input list
i_think(,NULL).
i_think([X|Xs],S):-
Y = Xs,
Y1 = X,
concatenate([X,Y1],Z1),
i_think(Y,T),
i_think(Xs,U),
S = [Z1,T,U].
%
concatenate([X,Y],R):-
atomics_to_string([X,Y],T),
atomics_to_string([Y,X],U),
R= [T,U].
%
i_exists(X,Y,false):-
len(X,Y,2);
len(X,Y,1).
i_exists(X,Y,true):-
len(X,Y,0).
%
len(X,Y,Count) :-
length(X,K),
length(Y,I),
(
(
K < I,
Count is 0
);
(
I < K,
Count is 2
);
(
I = K,
Count is 1
)
).
The main problem is with i_think/2, thanks for any help.
....
Specify I/O, of course, and thanks for replying:
descartes/3 :
descartes(['gold','bank'],Result,isExist)
Result=['goldgold','goldbank','bankbank','bankgold']
isExist= true
(In fact I would prefer to erase repetitions as...)
descartes(['gold','gold'],Result,isExist)
Result=['goldgold','goldgold','goldgold','goldgold']
(this shouldn't be generated as i_exist would return true)
isExist= false
prolog
prolog
edited Nov 16 '18 at 13:04
davidza5
asked Nov 16 '18 at 3:19
davidza5davidza5
64
64
I ran this and found?- descartes([a,b],L,R). L = [["aa", "aa"], [["bb", "bb"], _6960, _6966], [["bb", "bb"], _7038, _7044]], R = true ;
. Can you give us a few sample input and expected output so we know what is suppose to happen. I ran your code and while I do see some combinations being generated, I see lots of extra undefined variables being created and have no idea what they are for. When you give the examples please do them for all of the predicates you created,descartes/2
,i_think/2
,i_exists/3
– Guy Coder
Nov 16 '18 at 10:47
FYI,NULL
is a variable since it begins with a capital letter.
– lurker
Nov 16 '18 at 14:36
add a comment |
I ran this and found?- descartes([a,b],L,R). L = [["aa", "aa"], [["bb", "bb"], _6960, _6966], [["bb", "bb"], _7038, _7044]], R = true ;
. Can you give us a few sample input and expected output so we know what is suppose to happen. I ran your code and while I do see some combinations being generated, I see lots of extra undefined variables being created and have no idea what they are for. When you give the examples please do them for all of the predicates you created,descartes/2
,i_think/2
,i_exists/3
– Guy Coder
Nov 16 '18 at 10:47
FYI,NULL
is a variable since it begins with a capital letter.
– lurker
Nov 16 '18 at 14:36
I ran this and found
?- descartes([a,b],L,R). L = [["aa", "aa"], [["bb", "bb"], _6960, _6966], [["bb", "bb"], _7038, _7044]], R = true ;
. Can you give us a few sample input and expected output so we know what is suppose to happen. I ran your code and while I do see some combinations being generated, I see lots of extra undefined variables being created and have no idea what they are for. When you give the examples please do them for all of the predicates you created, descartes/2
,i_think/2
,i_exists/3
– Guy Coder
Nov 16 '18 at 10:47
I ran this and found
?- descartes([a,b],L,R). L = [["aa", "aa"], [["bb", "bb"], _6960, _6966], [["bb", "bb"], _7038, _7044]], R = true ;
. Can you give us a few sample input and expected output so we know what is suppose to happen. I ran your code and while I do see some combinations being generated, I see lots of extra undefined variables being created and have no idea what they are for. When you give the examples please do them for all of the predicates you created, descartes/2
,i_think/2
,i_exists/3
– Guy Coder
Nov 16 '18 at 10:47
FYI,
NULL
is a variable since it begins with a capital letter.– lurker
Nov 16 '18 at 14:36
FYI,
NULL
is a variable since it begins with a capital letter.– lurker
Nov 16 '18 at 14:36
add a comment |
0
active
oldest
votes
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%2f53330938%2fgetting-all-combinations-of-elements-in-a-list-prolog%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53330938%2fgetting-all-combinations-of-elements-in-a-list-prolog%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
I ran this and found
?- descartes([a,b],L,R). L = [["aa", "aa"], [["bb", "bb"], _6960, _6966], [["bb", "bb"], _7038, _7044]], R = true ;
. Can you give us a few sample input and expected output so we know what is suppose to happen. I ran your code and while I do see some combinations being generated, I see lots of extra undefined variables being created and have no idea what they are for. When you give the examples please do them for all of the predicates you created,descartes/2
,i_think/2
,i_exists/3
– Guy Coder
Nov 16 '18 at 10:47
FYI,
NULL
is a variable since it begins with a capital letter.– lurker
Nov 16 '18 at 14:36