To learn a little about regular expressions in Prolog, I'm trying to write functions that determine whether the input fits the pattern; my functions are as follows:
split(W, [[], W]).
split([X|W], [[X|W1], W2]) :- split(W, [W1, W2]).
match(eps, []).
match(symb(A), [ A ]).
match(union(R1, R2), W) :- match(R1, W).
match(union(R1, R2), W) :- match(R2, W).
match(conc(R1, R2), W) :- split(W, [W1, W2]), W1 \= [], W2 \= [], match(R1, W1), match(R2, W2).
match(star(R), W) :- match(R, eps).
match(star(R), W) :- split(W, [W1, W2]), W1 \= [], match(R, W1), match(star(R), W2).
I enter into SWIPL the following and get the following results:
?- match(star(symb(a)),[a,a,a,a]).
false.
?- match(star(symb(b)),[b]).
false.
As far as I can tell, the other functions are working correctly. Can somebody tell me where I went wrong with handling star?
Thank you!
bbbbbbwith regexa*b. If you have learnt about cut, you can also make the program output true and end without going through all other possible cases. - nhahtdh