For given a list L, append/3
finds all solutions such that append(X, Y, L)
succeeds.
% ?- append(X, Y, [a,b]).
%@ X = [],
%@ Y = [a, b] ;
%@ X = [a],
%@ Y = [b] ;
%@ X = [a, b],
%@ Y = [] ;
%@ false.
I expected similar befhaviour of the well known smart_reverse/3
smart_reverse([], X, X).
smart_reverse([A|X], Y, Z):- smart_reverse(X, [A|Y], Z).
However, for a given list L, query smart_reverse(X, Y, L)
goes to endless looping.
% ?- smart_reverse(X, Y, [a, b]).
%@ X = [],
%@ Y = [a, b] ;
%@ X = [a],
%@ Y = [b] ;
%@ X = [b, a],
%@ Y = [] ; <=== looping
There is a simple workaround for this by
non_smart_reverse(X, Y, Z) :- append(A, B, Z),
reverse(B, Y).
% ?- non_smart_reverse(X, Y, [a,b]).
%@ Y = [b, a] ;
%@ Y = [b] ;
%@ Y = [] ;
%@ false.
I don’t like help of append
here for some reason, and would like to find a more direct solution.
Of course I noticed the query ?- append(X, Y, Z)
with variable X, Y, Z goes endless for infinitely many solutions.
I appreciate if someone shows a smart reverse codes in pure prolog without use of append like the above.