Re: my perm/2 is not commutative
- From: skral@xxxxxxxxxxxxxxxxxxxxxxxxxx (Kral Stefan)
- Date: 13 May 2005 13:31:19 GMT
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/
.
- References:
- my perm/2 is not commutative
- From: Anders Lindén
- my perm/2 is not commutative
- Prev by Date: Re: Adding to the end of a list
- Next by Date: Re: Adding to the end of a list
- Previous by thread: my perm/2 is not commutative
- Next by thread: Adding to the end of a list
- Index(es):