Prolog Course Section: Verifying Palindromes for Input Lists

Prolog Course Section: Verifying Palindromes for Input Lists

Verified Sources
Nov 4, 2026

A palindrome is a sequence that reads the same forwards and backwards (e.g., [r,a,c,e,c,a,r][r,a,c,e,c,a,r]). 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 [H∣T][H|T].
  • 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)

  1. 1
    Step 1

    Create palindrome/1 that succeeds when the list is a palindrome.

  2. 2
    Step 2

    Make palindrome/1 succeed for [] and [_] because both are palindromes.

  3. 3
    Step 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).

  4. 4
    Step 4

    If First == Last, remove both outer elements and recurse on the inner list.

  5. 5
    Step 5

    Write a predicate that calls palindrome/1 and prints Yes when it succeeds, otherwise No.

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 L=[]L=[] or L=[]L=[_ ], palindrome/1 succeeds immediately.
  • Otherwise:
    1. Bind the first element to First.
    2. Use last_elem/3 to get Last and WithoutLast.
    3. Require First = Last.
    4. Remove the first element from WithoutLast to obtain the inner list Inner.
    5. Recurse.

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]). prints Yes
  • ?- check_palindrome([a,b,c]). prints No

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:

  1. Compute the reversed list.
  2. 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 1

Understand base cases and how to peel list structure."

Outer-to-inner reasoning

Step 2

Compare first and last elements, then recurse on the inner list."

Helpers for last element

Step 3

Implement last extraction to remove the last element safely."

User-facing Yes/No output

Step 4

Wrap 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

1 / 5
Question · Term

What should palindrome/1 return for []?

Click to reveal
Answer · Definition

True. Empty list reads the same forward and backward.

Knowledge Check

Question 1 of 3
Q1Single choice

Which predicate definition is a correct palindrome base case?