How to program a list in Prolog that can contain only the character a but must finish with the character b?

I want to program a list that uses only the characters {a, b}.

My objective is that prolog returns true only if the list that the user enter contains n number of a, or at least one a but has to finish with one b only, no more and no less than just one b.

Example: aaab is correct, aba is incorrect, a is incorrect, b is incorrect and the list empty which is [] is also correct.

Here’s my code :

langage([a | S]) :-

The problem here is that it only accepts n numbers of a, and does not finish with b. But I want it to finish with the letter b.

I hope someone can help me.

Edit by @EricGT

This is cross posed on StackOverflow (ref)

Your current code always ends with an empty item, I.e.


What if it ended with b? , I.e.


What if it ended with ab? , I.e.


That needs fixing but to what I am not sure.

Actually langage([]). means that the list empty is correct.
This means if you type language([]). then it’s true.