element11
element11

Reputation: 4475

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.

Upvotes: 2

Views: 594

Answers (1)

gusbro
gusbro

Reputation: 22585

You can add a new clause to ignore spaces:

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

Upvotes: 4

Related Questions