Prolog: how to write (and use) a function that lists all list permutations?

This is truly a naive sort -- it traverses the tree of all possible permutations until it luckily finds a sorted one. That's have a complexity of O(n! ) I presume.

Up vote 3 down vote favorite share g+ share fb share tw.

I've found such an example of naive sort written in prolog and I am trying to understand it: naive_sort(List,Sorted):-perm(List,Sorted),is_sorted(Sorted). Is_sorted(). Is_sorted)_).

Is_sorted(X,Y|T):-X= Delete(X,X|T,T). Delete(X,H|T,H|NT):-delete(X,T,NT). Naive_sort call works correctly but I just can't figure out why.

The main problem is the permutation. When it is called implicitly it always returns only one value. How is it then possible that in naive_sort function call all permutations are checked?

Also how could I modify perm function to write all permutations? Prolog link|improve this question edited Feb 4 '10 at 1:14 asked Feb 4 '10 at 0:32agnieszka2,23532865 73% accept rate.

This is truly a naive sort -- it traverses the tree of all possible permutations until it luckily finds a sorted one. That's have a complexity of O(n! ) I presume :> About the permutation function -- it works "backwards" -- note that the definition takes the head out of the result.

If you turn around your point of view, you'll notice that instead of deleting it actually inserts values by working backwards. As the algorithm is working backwards, hence the Head chosen may be anything that will allow a result to be created, hence any unused value from List. Basically the permutation algorithm translates to the following procedural implementation: pick an item from List put it into front of Sorted This way you generate permutations.

All of them. In short - perm generates the whole space of possible solutions by starting out of an empty solution and checking how the given solution is possible from a valid delete.? - perm( 1, 2, 3 , P ) P = 1, 2, 3; P = 1, 3, 2; P = 2, 1, 3; P = 2, 3, 1; P = 3, 1, 2; P = 3, 2, 1; no.

– agnieszka Feb 4 '10 at 0:55 @agnieszka - it's not a function! It's a predicate that is true ONLY if the first argument is a permutation of the second. – Kornel Kisielewicz Feb 4 '10 at 0:59 @agnieszka - using GNU Prolog or SWI?

– Kornel Kisielewicz Feb 4 '10 at 1:01 YESSS I know that, still you've written "it generates permutations. All of them. " - it does not.

It finds first combination of the unknown arguments that satisfies the predicate - so only ONE permutation. So how, how is it possible that it works when calling naive_sort? – agnieszka Feb 4 '10 at 1:02 using SWI------- – agnieszka Feb 4 '10 at 1:03.

The main problem is the permutation function. When it is called implicitly it always retrns only one value. Prolog is a language that always attempts on proving the truth of a statement, by deriving it using the axioms (facts or rules) given.

Perm is not a function in the sense of procedural programming. Perm is a predicate, about which we tell prolog two things: The empty list is a permutation of itself. List is a permutation of H|Perm if there is a list Rest such that Rest is obtained by deleting H from List, and Rest is a permutation of Perm.

When asked whether some list is a permutation of another, prolog will attempt to apply these derivation steps (recursively) to prove it. If this recursion reaches a dead end, i.e. A statement which can not be proven as no rules can be applied to it, it backtracks.

I knew all that except of the backtracking if anything fails - this explains a lot – agnieszka Feb 4 '10 at 1:17 Describing it is so much easier if english is one's native language -_- – Kornel Kisielewicz Feb 4 '10 at 1:19 I take that as a compliment, because it is not my native language either :-) – meriton Feb 4 '10 at 1:26 +1: if that's the case ;) – Kornel Kisielewicz Feb 4 '10 at 1:28.

This is truly a naive sort -- it traverses the tree of all possible permutations until it luckily finds a sorted one. That's have a complexity of O(n!) I presume.

Prolog is a language that always attempts on proving the truth of a statement, by deriving it using the axioms (facts or rules) given.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions