
% ---------------
% myreverse(L,R).
% ---------------
% succeeds iff R is the reverse of list L,
%    either or both arguments can be instantiated

% the reverse of a one or two element list is just itself
myreverse([],[]).
myreverse([E],[E]).

% to obtain the reverse of larger lists,
%    reverse the tail of the list to get R1
%        and then append the head after that
% (put H as the sole element of the list to be appended)
myreverse([H|T],R) :- myreverse(T,R1), append(R1,[H],R).


% -------------
% tailRev(L,R).
% -------------
% succeeds iff R is the reverse of list L,
%    done with a tail-recursive reverse,
%
% uses one extra parameter to keep track of
%    what has been reversed so far (Prev)
%
% either or both arguments can be instantiated

tailRev([], Prev, Prev).
tailRev([H|T], New, Prev) :- tailRev(T, New, [H | Prev]).
tailRev(L, Result) :- tailRev(L, Result, []).

