0

What is permutation/2 ?

As said in the help(permutation).:

The predicate permutation/2 is primarily intended to generate permutations.

That's what I want to use it for.


How permutation/2 should work

This is the example given in the documentation: (Of course, the box around does not exist normally.)

 ____________________________________________________________________|                                                                          |
| ?- permutation([1,2], [X,Y]).                                      |
| X = 1, Y = 2 ;                                                     |
| X = 2, Y = 1 ;                                                     |
||false.____________________________________________________________ ||

Reproducing the example (and several other examples)

When I try permutation/2, not the same happens: (any of these are run on my computer)

?- permutation([1,2],[X,Y]).
X = 1,
Y = 2 .

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

?- permutation([1,2],[2,1]).
true .

As you can see, prolog doesn't return any permutation but the input itself.

  • Why is this happening?
  • And how to get all permutations?

More details on my prolog version.

  • On my Ubuntu/Linux computer
  • I installed the package swipl just yesterday
  • it tells me it's version: threaded, 64 bits, version 7.4.2
false
  • 10,264
  • 13
  • 101
  • 209
Asqiir
  • 607
  • 1
  • 8
  • 23
  • @ Asqiir my Prolog version SWI-Prolog (threaded, 64 bits, version 7.4.0-rc1).Windows 7. it works as prescribed.... – Anton Danilov May 03 '18 at 06:11
  • 6
    Look very closely at the transscript! There, it reads `X = [2, 1 ]**SPACE**.`. You have stopped Prolog from giving the next answer. Try to press `;` or `SPACE` instead. – false May 03 '18 at 06:46
  • 1
    The interpreter is basically asking you if you are happy with the answer. By pressing enter or `.`, you are saying "I am happy" and Prolog doesn't do anything else. If you press `;` or space, you are saying "no, this isn't it" and Prolog will then backtrack and give you another result. – Daniel Lyons May 03 '18 at 14:46
  • If you want all of them in a list, you can do `setof(X,permutation([1,2,3],X),Xs).`. – Marijn Jun 20 '18 at 17:22

0 Answers0