%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- fundamental -*-
% fftwgel_auxcode.pl
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Description:	In this module several general-purpose predicates are defined.
% Language:	SICStus Prolog 3
% Copyright:	2003, Stefan Kral
% Author: 	Stefan Kral (URL mailto:skral@complang.tuwien.ac.at)
% License:	GNU General Public License (GPL) Version 2 (or higher)
% History:	Fri Feb 21 11:48:38 CET 2003: created
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

member_of(X, [X|_Xs]).
member_of(X, [_Z|Xs]) :-
   member_of(X, Xs).

member_of_rest(X, [X|Xs], Xs).
member_of_rest(X, [Z|Xs], [Z|Zs]) :-
   member_of_rest(X, Xs, Zs).

same_length([], []).
same_length([_X|Xs], [_Z|Zs]) :-
   same_length(Xs, Zs).

list_reversed(Xs, RevXs) :-
   same_length(Xs, RevXs),			% ensures universal termination
   list_accu_reversed(Xs, [], RevXs).

list_accu_reversed([], As, As).
list_accu_reversed([X|Xs], As, Rs) :-
   list_accu_reversed(Xs, [X|As], Rs).

list_nonmember([], _X).
list_nonmember([Z|Zs], X) :-
   dif(Z,X),
   list_nonmember(Zs, X).

list_permutation(Xs, Zs) :-
   same_length(Xs, Zs),				% ensures universal termination
   list_permutationHLP(Xs, Zs).

list_permutationHLP([], []).
list_permutationHLP([X|Xs], PermXXs) :-
   member_of_rest(X, PermXXs, PermXs),
   list_permutationHLP(Xs, PermXs).

ensure_(Goal, Exception) :-
   if(call(Goal), true, raise_exception(Exception)).
