Recently, Greg Weber blogged about a Haskell version of the regex matcher that Brian Kernighan wrote about in Beautiful Code.
The example is written in C. I wondered if a more beautiful version could be created using a newer programming language. Kernighan points out that it takes more code to produce the equivalent in Java, which is not suprising. But he also points to the strengths of C. “Pointers … are used to create compact expressions that naturally express the extracting of individual characters and advancing to the next character. Array indexing of substrings can achieve the same effect, but in this code, pointers do a better job …” But we can maintatin the advantages of pointers and work at a slightly higher level by using lists. Since this code also uses recursion and pattern matching, Haskell is a very natural replacement.
