Iam trying to generate all n-element sublists of a given list n is one of the arguments I have to use enforced backtracking but not getting the idea

I’m using: SWI-Prolog version 8.3.0

I want the code to: generate all n-element sublists of a given list n is one of the arguments I have to use enforced backtracking but not getting the idea

The output has to be like this

?- choose(1, [1,2,3], X).
X = [1] ;
X = [2] ;
X = [3] ; 
false

But what I’m getting is:

?- choose(1, [1,2,3], X).
 X = [1]

My code looks like this:

choose(0, [], []).
choose(Len, [E|Tail], [E|NTail]):-   
    succ(PLen, Len),
    (PLen > 0 -> choose(PLen, Tail, NTail) ; NTail=[]).
 choose(Len, [_|Tail], NTail):-
   choose(Len, Tail, NTail).

Here is some output from a quick example I did:

?- choose(0,[1,2,3],R).
R = [].

?- choose(1,[1,2,3],R).
R = [1] ;
R = [2] ;
R = [3] ;
false.

?- choose(2,[1,2,3],R).
R = [1, 2] ;
R = [1, 3] ;
R = [2, 3] ;
false.

?- choose(3,[1,2,3],R).
R = [1, 2, 3] ;
false.

?- choose(4,[1,2,3],R).
false.

A few days ago someone posted this: What does this code do?

I guess one could also say it helps to answer your question.

That code with length/2 is about all you need.

Answer - Click to remove blur

choose(N,L,Ps) :-
    p(Ps,L),
    length(Ps,N).

oh thats great you got the correct answer can you help me join this code Actually Iam new to prolog

I do not understand what that means.

Sorry for the typo actually I was asking if you can paste your solution here

I tried the answer you suggested but i think it is giving me the same output as I had previosuly

The answer was posted under the blur in the earlier post.

Here is all of the code.

Click to remove blur.

p([],[]).
p([X|Xs],[X|Ys]) :-
    p(Xs,Ys).
p(Xs,[_|Ys]) :-
    p(Xs,Ys).

choose(N,L,Ps) :-
    p(Ps,L),
    length(Ps,N).

Since you are new to Prolog you might find the Prolog Visualizer of use.

Note that queries end with a ? and it does not do cut, but for learning very basic Prolog and unification it is very good.

This same solution is not working for me like it only gives one solution. But i should give more subsets it available but i gives only 1

You need to press the space bar when you see a returned result that ends with ;. The ; means or, meaning this is the answer or there is another answer. Make sense?

?- choose(2,[1,2,3],R).
R = [1, 2] ; Press space bar after seeing ;
R = [1, 3] ; Press space bar after seeing ;
R = [2, 3] ; Press space bar after seeing ;
false.

Yes I got your point but I was just wondering if there is any solution like if we get the whole solution by not pressing the space bar

I guess you mean you would like to collect all of the results into a list.

See: Finding all Solutions to a Goal

However, since you noted you are new to Prolog, please don’t start asking many questions about this until you learn more. It could take some rather lengthy answer(s) to cover all of the information needed to really understand the use of those predicates especially the parts about

The construct +Var^ Goal tells bagof/3 not to bind Var in Goal.

oh okay got something of it and yes i was trying to get all these solutions as list
thank-you for helping me
God Bless