Re: my perm/2 is not commutative



Hi Anders.

Anders Lindén (xxxx@xxxxxx) wrote:
: I have written a permutation relation that is like this:
:
: perm([],[]).
: perm(L,[A|B]):- insert(A,X,L), perm(X,B).
:
: It works in one direction: [...] : but not in this direction: [...]
: ...then it will produce one solution and enter an eternal loop.

: How can I write my perm relation more general?
The predicate does not universally terminate in both modes.

The conjunction of the goal same_length/2 (asserting that two lists
have equal length) and the goal perm/2 is probably what you want.

perm_of(Xs,Ys) :- same_length(Xs,Ys), perm(Xs,Ys).

On most Prolog systems, this additional step will also slightly
improve the performance (if you are interested in a large number
of permutations).

Regards,
Stefan.

--
Stefan Kral http://www.complang.tuwien.ac.at/skral/
.