When I first posted the simple regex code I thought that it contained no errors. But when I tried it a few days after that with a few simple testcases (which I hadn’t considered in the test code) it failed.
ok $ match "world$" "world"
ok $ match "world$" " world"
ok $ match "world" "hello world"
The first testcase worked. The other two didn’t, but they should have. After a
bit of inspection with the trace
function in Debug.Trace
I found out that
the function couldn’t move past the first character. I thought matchhere
would
take care of that, but it didn’t.
The change that is needed was in match
. These lines have the error.
match reg s@(x:xs) = if matchhere reg s
then True
else matchhere reg xs
The function only tried to match at the first and second position.
match reg s@(x:xs) = if matchhere reg s
then True
else match reg xs
By changing matchhere
to match
, now matchhere
will be called on each
character of the string. Because this way the end of the string can be reached,
I have to the two lines to check for the end of the string and the regex.
match [] _ = True
match _ [] = False
It seems that the code works now like it should.