Prolog. Could someone help me with this and please give a brief explanation of e
ID: 3866152 • Letter: P
Question
Prolog. Could someone help me with this and please give a brief explanation of each step.
Actually I've already been told this:
palindrome(List1):-
findrev(List1,[],List2),
compare(List1,List2).
findrev([],List1,List1).
findrev([X|Tail],List1,List2):-
findrev(Tail,[X|List1],List2).
compare([],[]):-
write(" List is Palindrome").
compare([X|List1],[X|List2]):-
compare(List1,List2).
compare([X|List1],[Y|List2]):-
write(" List is not Palindrome").
and this works but when I load it it says there are singleton variables [X, List1, Y, List2] and also I dont need the "List is/is not a Palindrome" just need to match the teacher's examples. Replacing this text with "True" or "False" will not work, it gives a double answer "False true" for a non-palindrome.
If I try and remove the rules with the write command in them, whole program doesn't work gives false instead of true, I don't even know how removing it changes things.
Replacing this text with "True" or "False" also will not work, eg it gives a double answer "False true" for a non-palindrome.
Also it would be great to have an brief explanation of each step. Thank you!
Explanation / Answer
palindrome(List1):-
findrev(List1,[],List2),
compare(List1,List2).
findrev([],List1,List1).
findrev([X|Tail],List1,List2):-
findrev(Tail,[X|List1],List2).
compare([],[]):-
write(" List is Palindrome").
compare([X|List1],[X|List2]):-
compare(List1,List2).
compare([X|List1],[Y|List2]):-
write(" List is not Palindrome").
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.