Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!pipex!uunet!sparky!kwiudl.kwi.com!usenet
From: tom@kwi.com (Tom Howland)
Subject: Re: Permutation Predicate in Prolog.
In-Reply-To: tjdec@westminster.ac.uk's message of Mon, 14 Nov 1994 13:11:41 GMT
Message-ID: <TOM.94Nov15091709@heather.kwi.com>
Lines: 29
Sender: usenet@kwiudl.kwi.com
Organization: KnowledgeWare Inc, Redwood City, CA 94065-1417
References: <Cz9DzI.1GB@westminster.ac.uk>
Date: Tue, 15 Nov 1994 17:17:09 GMT

>>>>> "KB" == Kaustav Bhattacharya <tjdec@westminster.ac.uk> writes:
In article <Cz9DzI.1GB@westminster.ac.uk> tjdec@westminster.ac.uk (Kaustav Bhattacharya) writes:


    KB> Can anybody help me out with creating a permutation predicate in
    KB> Prolog.  I'm really stuck.  All it needs to do is take in a list
    KB> of numbers and produce a permutation of all the numbers in every
    KB> posible order.

    KB> Kaustav.

From Quintus's library(lists)

%   permutation(?List, ?Perm)
%   is true when List and Perm are permuations of each other.
%   Be careful: this is quite efficient, but the number of permutations of an
%   N-element list is N!, and even for a 7-element list that is 5040.

permutation(List, Perm) :-
	permutation(List, Perm, Perm).

permutation([], [], []).
permutation([X|Xs], Ys1, [_|Zs]) :-
	permutation(Xs, Ys, Zs),
	insert(Ys, X, Ys1).

insert(L, X, [X|L]).
insert([H|T], X, [H|L]) :-
	insert(T, X, L).
