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, b is incorrect, a is incorrect.
Here's my code :
langage([]).
langage([a | S]):-
langage(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.
You could write it like this:
So your list needs to end on
aandb, any leadinga's will be stripped by the second rule. Tested with SWISH:However, if you wanted to have a list starting with
awith n>=1 follow upb, try this (not tested):