Prolog Course Section: Verifying Palindromes for Input Lists
A palindrome is a sequence that reads the same forwards and backwards (e.g., ). In Prolog, this is naturally expressed with recursion and pattern matching on lists: compare the first and last elements, then recurse on the inner sub-list. This section builds a predicate such as palindrome/1 and a wrapper goal that prints Yes/No.
Key ideas you’ll use:
- Use list decomposition via pattern matching like .
- Define base cases (empty list, single-element list).
- Recurse by removing matching outer elements.
- Optionally handle the “middle” correctly by stopping when the list becomes length 0 or 1.
⚠️ Note: Prolog can implement palindrome checking either by (1) reversing a list and unifying, or (2) recursively matching from both ends without a full reverse. This course uses a recursive approach that is conceptually clear for learning, while also mentioning a reverse-based option.
Prolog lists (pattern matching & recursion) tutorial
Build a Prolog palindrome verifier (recursive, outer-to-inner)
- 1Step 1
Create palindrome/1 that succeeds when the list is a palindrome.
- 2Step 2
Make palindrome/1 succeed for [] and [_] because both are palindromes.
- 3Step 3
For a list like [X|Rest], you need both the first element X and the last element (e.g., via a helper that returns the last element and remaining tail).
- 4Step 4
If First == Last, remove both outer elements and recurse on the inner list.
- 5Step 5
Write a predicate that calls palindrome/1 and prints
Yeswhen it succeeds, otherwiseNo.
1) Core definition (palindrome using outer-element comparison)
In Prolog, list pattern matching lets you reason in terms of “outer” elements. A standard technique is to implement helpers that:
- extract the last element of a list, and
- return the list without that last element (so you can recurse on the inner portion).
Example program
1% --- Helper: last element and the list before it --- 2% last_elem([a,b,c], c, [a,b]). 3last_elem([X], X, []). 4last_elem([H|T], X, [H|Rest]) :- 5 last_elem(T, X, Rest). 6 7% --- Palindrome predicate --- 8% palindrome([]). 9% palindrome([_]). 10palindrome([]). 11palindrome([_]). 12palindrome(List) :- 13 List = [First|_], 14 last_elem(List, Last, WithoutLast), 15 First = Last, 16 % WithoutLast now is the list with last removed; remove the first too by taking its tail 17 WithoutLast = [_|Inner], 18 palindrome(Inner).
How it works
- For or , palindrome/1 succeeds immediately.
- Otherwise:
- Bind the first element to
First. - Use last_elem/3 to get
LastandWithoutLast. - Require
First = Last. - Remove the first element from
WithoutLastto obtain the inner listInner. - Recurse.
- Bind the first element to
keyword In Prolog,
=is unification (it can bind variables, or check equality for already-bound terms).
type="tip"
title="Tip: Test with concrete queries"
content="Use queries like ?- palindrome([r,a,c,e,c,a,r]). and ?- palindrome([a,b,c]). to quickly validate base cases and recursion."
2) Match the exact assignment output: Yes / No
Prolog itself typically answers with true/false, but you can add a wrapper that prints Yes or No based on whether palindrome/1 succeeds.
1check_palindrome(List) :- 2 ( palindrome(List) -> 3 writeln('Yes') 4 ; 5 writeln('No') 6 ).
Example usage
?- check_palindrome([r,a,c,e,c,a,r]).printsYes?- check_palindrome([a,b,c]).printsNo
keyword A goal is a predicate call you ask Prolog to prove (e.g.,
palindrome(List)).
type="warning" title="Warning: Be careful when removing outer elements" content="In the recursive step, you must remove BOTH the first and last element before recursing. If you only remove the last (or only the first), the recursion won’t shrink correctly and may fail or loop."
3) Alternative (simpler) approach: compare to reverse
A more compact approach is:
- Compute the reversed list.
- Check whether the original equals the reversed list.
However, this can be less instructive because it relies on reverse/2 or a custom reverse implementation.
Pseudocode idea:
palindrome(L) :- reverse(L, R), L = R.
If you want the “learning” version, prefer the outer-to-inner recursion shown earlier.
Learning Roadmap for Palindrome Verification in Prolog
List recursion fundamentals
Step 1Understand base cases and how to peel list structure."
Outer-to-inner reasoning
Step 2Compare first and last elements, then recurse on the inner list."
Helpers for last element
Step 3Implement last extraction to remove the last element safely."
User-facing Yes/No output
Step 4Wrap palindrome/1 with a printing predicate."
Common Questions & Edge Cases
Palindrome Check: Expected Results
Compare provided goals and whether palindrome/1 should succeed.
Palindrome in Prolog — Quick Self-Test Deck
Knowledge Check
Which predicate definition is a correct palindrome base case?