Search code examples
prologspacespalindromedcg

Prolog: Ignoring spaces using dcg rules


I have written a program in SWI Prolog to test if a string is a palindrome. This task in DCG I was able to figure out, and was quite trivial.

palindrome --> [X], palindrome, [X].  
palindrome --> [X],[X]. 
palindrome --> [X].   

I would like to write a rule that will ignore spaces, but after searching for a while nothing really matched what i was looking for. I am still pretty new to DCG, any insight would be greatly appreciated.


Solution

  • You can add a new clause to ignore spaces:

    palindrome --> " ", palindrome.
    ?- phrase(palindrome, "ab cba",[]).
    true