I'm new to Prolog, and I have an exercise which asks to make a palindrome predicate that returns a Boolean value? This boolean is true if the list is Palindrome otherwise returns false
palindrom([X|Xs],bool) .
How I should do it ?
firstly, it's not function it's a predicate, so palindrom is like to compare list with the invert of it like L=[x,y,x], L1=[x,y,x] then you shoud invert list befor ,
code
palindrom(Xs):-palindrom(Xs,Xs,[]). % the second Xs is copy of Xs in input and [] is the auxliare
palindrom([],Xs,Xs).
palindrom([Y|Ys],Xs,Zs):- palindrom(Ys,Xs,[Y|Zs]).
version2
palindrom(Xs) :- reverse(Xs,Xs).
Note :reverse/2 to invert list but in this predicate return boolean value because the second is an intput to invert list
result
| ?- palindrom([a,b,c]).
no
| ?- palindrom([a,b,a]).
yes
for more details use ?-trace,palindrom("radar").
to see how it's work inside