Monday 31 October 2016

Write a Prolog program to implement reverse(List,ReversedList) that reverses lists.

% Write a Prolog program to implement reverse(List,ReversedList) that reverses
% lists.


/* Reverse of the list. */

reverse([H|T],R):-
 length(T,L),
 L>0 ->
 (
  reverse(T,R1),
  /* write(R1), */
  R is H
 )
 ;
 R is H.

% Output
Prolog program to implement reverse(List,ReversedList) that reverses lists.
Prolog Program to reverse the elements in the list.

5 comments:

  1. wrong output :
    1 ?- reverse([1,2,3,4],R).
    R = 1.

    ReplyDelete
    Replies
    1. Bro it's working. You can see the Output image.

      Delete
  2. Sorry to say, but this code is not showing the correct output

    ReplyDelete
    Replies
    1. Can you please mention the case for which you're not getting the correct output?

      Delete