Prolog. Could someone help me with this and please give a brief explanation of e
ID: 3866137 • 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. 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.
Also it would be great to have an brief explanation of each step. Thank you!
whether it is read forwards or backwards ?- palindrome ([a,b,c,c,b,a]) true. ?- palindrome ([1,3,2,3,11) true ?- palindrome[a,b,c,a,b,c]). false.Explanation / Answer
domains
list=symbol*
predicates
palindrome(list)
reverse(list,list,list)
comp(list,list)
clauses
palindrome(List1):-
reverse(List1,[],List2),
comp(List1,List2).
reverse([],List1,List1).
reverse([X|Tail],List1,List2):-
reverse(Tail,[X|List1],List2).
comp([],[]):-
write(" True").
comp([X|List1],[X|List2]):-
comp(List1,List2).
comp([X|List1],[Y|List2]):-
write(" False").
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.