-1
%reverse_List
reverseList(H|T,ReversedList):-
    reverseListHelper(T,[H],ReversedList).
reverseListHelper([],Accumulator.Accumulator).
reverseListHelper([H|T],Accumulator,ReversedList):-
    reverseListHelper(T,[H|Accumulator],ReversedList).

I am beginner to prolog, what wrong with this code ? it's just giving the output false kindly explain thanks

I understand the theory of how recursive works in list reversing but not the code much, if any one could explain line by line would be great thanks

rdas
  • 20,604
  • 6
  • 33
  • 46
  • Three typos. In `Accumulator.Accumulator` use a `,` instead. Then add `[` and `]` around `H|T` in first clause. Add a fact for the empty list, – false Nov 06 '22 at 11:53

1 Answers1

0

One can look at a list in Prolog as essentially a FIFO (first-in/first-out) stack: you can examine, add or remove things only from the top/head: the remainder of the list is opaque.

Reversal of such a list, then, consists of repeatedly (and recursively) popping items from the source stack and pushing each such popped item onto a result stack.

And a common idiom in prolog is the use of a helper predicate with the same name, but different arity, where the additional argument(s) carry whatever extra state is required to solve the problem. In this case, the extra state needs is a list onto which we can push/prepend things as we go, thus building the result in reverse order.

That leads to an implementation of reverse_list/2 that looks like this:

reverse_list( Xs, Ys ) :- reverse_list( Xs , [] , Ys ) .

The helper that does all the work isn't much more complicated. There are just two cases:

  • The limiting case, where the source list is the empty list, and
  • The general case, where the source list is non-empty

The limiting case is easy: if the source list is exhausted, the accumulator, whatever it might contain, is the reversed list. That gives us

reverse_list( [] , Zs, Zs ) .

The general case merely involves

  • removing the head of the source list,
  • prepending it to the accumulator, and
  • recursing down on the tails.

Which leads to this (you might note that we leave the final result alone until we hit the limiting case here)

reverse( [X|Xs] , [X|Ys] , Zs ) :- reverse(Xs,Ys,Zs)

Putting it all together, we get

% reverse/2 ----------------------------------------------------------
%
% reverse( Source, Reverse )
%
% Simply invoke the helper predicate, seeding the accumulator with the
% empty list.
%
% --------------------------------------------------------------------
reverse( Xs , Ys ) :- reverse(Xs,[],Ys) .

% reverse/3 ----------------------------------------------------------
%
% reverse( Source, Accumulator, Reversed )
%
% --------------------------------------------------------------------
reverse( []     ,    Zs  , Zs ) .  % source list empty: unify accumulator and result
reverse( [X|Xs] , [X|Ys] , Zs ) :- % non-empty source? Put X on the accumulator, and
  reverse(Xs,Ys,Zs) .              % - recurse down on the tails.
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135