I am a beginner in Prolog and I am stuck in a homework assignment. I have to build a predicate myReverse(XS,YS)
where XS
is the reverse list of YS
. I have built some logic as follows:
myReverse([],[]).
myReverse([H],[H]).
myReverse([L|H],[H|T]) :- myReverse(L,T).
This kinda works, but the output is not quite what I want. Some examples:
myReverse(L,[]).
L = [] % ok, that's fine
myReverse(L,[a]).
L = [a] % still fine
myReverse(L,[a,b]).
L = [[b]|a] % expected [b,a]
myReverse(L,[a,b,c]).
L = [[[c]|b]|a] % expected [c,b,a]
...
Is there any way I can achieve the expected output without using a accumulator or third party implementations like append
?
Edit: This question is NOT a duplicate of Reversing a List in Prolog because I do NOT want to use accumulator. Also, this question is much more about the output format of my given solution than the solution itself.