# How to make the program give the other alternate outputs

**URL:** <https://swi-prolog.discourse.group/t/how-to-make-the-program-give-the-other-alternate-outputs/343>\
**Category:** Help!\
**Created:** [March 3, 2019, 12:12pm UTC](https://swi-prolog.discourse.group/t/how-to-make-the-program-give-the-other-alternate-outputs/343 "2019-03-03T12:12:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonitSamaddar](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ronitsamaddar/32/134_2.png) [@RonitSamaddar](https://swi-prolog.discourse.group/u/RonitSamaddar)\
**Post date:** [March 3, 2019, 12:12pm UTC](https://swi-prolog.discourse.group/t/how-to-make-the-program-give-the-other-alternate-outputs/343/1 "2019-03-03T12:12:14Z")

</div>

I’m using: SWI-Prolog Multi-threaded, 64 bits, Version 7.2.3

I want the code to:  
find all possible permutations of the given list. It must run select2() function for different possible values of H, thereby giving the different permutations. However each time by base case is fixed and therefore for empty list ([]) it must always return empty list.

But what I’m getting is:  
When I am running the Prolog Program , first time it gives the list itself which is correct, then when I try to find other possible permutations by using ‘;’ operator, for finding other alternatives, it tries to run the rule for the base case instead of the fact. I understand that maybe thats what ‘;’ does. However my requirement is to run the fact always for base case and every time the only variations should result from the select2() function whose output decides the working of the following permutation() function

My code looks like this:

```auto
permutation([],[]).
permutation(L,[H|T]):-
        select2(H,L,R),
        permutation(R,T).

select2(_,[],[]).
select2(X,[X|T],T):-!.
select2(X,[H|T],[H|L]):-
	select2(X,T,L).

```

My trace looks like this  
?- trace,permutation([3,2,1],L).  
Call: (8) permutation([3, 2, 1], \_G1620) ? creep  
Call: (9) select2(\_G1738, [3, 2, 1], \_G1750) ? creep  
Exit: (9) select2(3, [3, 2, 1], [2, 1]) ? creep  
Call: (9) permutation([2, 1], \_G1739) ? creep  
Call: (10) select2(\_G1741, [2, 1], \_G1753) ? creep  
Exit: (10) select2(2, [2, 1], [1]) ? creep  
Call: (10) permutation([1], \_G1742) ? creep  
Call: (11) select2(\_G1744, [1], \_G1756) ? creep  
Exit: (11) select2(1, [1], []) ? creep  
Call: (11) permutation([], \_G1745) ? creep  
Exit: (11) permutation([], []) ? creep  
Exit: (10) permutation([1], [1]) ? creep  
Exit: (9) permutation([2, 1], [2, 1]) ? creep  
Exit: (8) permutation([3, 2, 1], [3, 2, 1]) ? creep  
L = [3, 2, 1] ;  
Redo: (11) permutation([], \_G1745) ? creep  
Call: (12) select2(\_G1747, [], \_G1759) ? creep  
Exit: (12) select2(\_G1747, [], []) ? creep  
Call: (12) permutation([], \_G1748) ? creep  
Exit: (12) permutation([], []) ? creep  
Exit: (11) permutation([], [\_G1747]) ? creep  
Exit: (10) permutation([1], [1, \_G1747]) ? creep  
Exit: (9) permutation([2, 1], [2, 1, \_G1747]) ?  
…  
This is a part of the trace. As can be clearly seen, from the alternative output case, it goes into a computation which is yielding the results I want([2,1,\_G1747]).

I am a beginner here so please pardon any mistakes

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 3, 2019, 12:33pm UTC](https://swi-prolog.discourse.group/t/how-to-make-the-program-give-the-other-alternate-outputs/343/2 "2019-03-03T12:33:30Z")

</div>

What is the code for `select2`?  
(And it’s not a “function”, which usually means that it returns a single result; it’s a “predicate”.)

Anyway, if you look at your output, you’ll see that the last “Call” was `(11) permutation([], _G1745)`. This is what “Redo” tries to do. (I’m guessing this is your question about “tries to run the rule for the base case”). The _second_ clause also matches this, but without further information, I can’t tell what your problem is.

---

<div class="post-metadata">

**Author:** ![RonitSamaddar](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ronitsamaddar/32/134_2.png) [@RonitSamaddar](https://swi-prolog.discourse.group/u/RonitSamaddar)\
**Post date:** [March 3, 2019, 4:23pm UTC](https://swi-prolog.discourse.group/t/how-to-make-the-program-give-the-other-alternate-outputs/343/3 "2019-03-03T16:23:32Z")

</div>

Oh, I am very sorry. I forgot to include the code for select2. Anyway select2 selects one element from the list and gives the remaining list as output. So as we are giving a variable as the element to remove, I want it to work such that every time it takes one of the elements out and gives remaining list which is permuted on.

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [March 3, 2019, 5:41pm UTC](https://swi-prolog.discourse.group/t/how-to-make-the-program-give-the-other-alternate-outputs/343/4 "2019-03-03T17:41:20Z")

</div>

I think the cut (`!` in the second clause of `select/3`) is likely the problem; that removes the choice point there & prevents finding subsequent variations in the third clause.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 3, 2019, 8:23pm UTC](https://swi-prolog.discourse.group/t/how-to-make-the-program-give-the-other-alternate-outputs/343/5 "2019-03-03T20:23:50Z")

</div>

Also, the first clause of `select2` shouldn’t be there, unless you think that `select2(5, [], [])` is correct.
