A Beautiful Regex Matcher... In Haskell

by chromatic

I've been reading Beautiful Code, picking out chapters here and there as I have time. While reading Brian Kernighan's explanation of Rob Pike's regular expression program from The Practice of Programming, I had an idle thought. "Hey, that's a highly recursive program with complex behavior suitable for didactic purposes."

Of course, Kernighan says that almost verbatim in the text. He also says "It's a nicer example than Yet Another Fibonacci Sequence Generator."

So I ported it to Haskell. I don't promise it's necessarily great Haskell, and I wouldn't consider it entirely beautiful, but it appears to function.

Another complaint I would have is that the names "text" and "regexp" make the original C code much easier to understand. When reading the Haskell code I often got confused between what the x's and y's were supposed to represent.