Parsec is a beautiful library, but it’s DIFFICULT. Every time I’m working on a problem that requires an actual parser beyond regex, I die a little inside. I used Parsec for my Scheme interpreter, and it was probably the most frustrating part of the project. This isn’t the fault of the authors, though. Parsers are complicated beasts, and I definitely didn’t know what I was doing the first time around. There’s an interesting problem on Codewars – given an arbitrary molecule string, return the count of each constituent atom. Sounds pretty simple, right? That’s it, we’re done. End of post. Obviously, we’re not done. Even organic chemists deal with atoms beyond C, H, O, N. To make matters more complicated, chemists are just like programmers – lazy. For polymeric materials with repeating groups, there’s a handy way to express their structure. For example, the formula for PVC is C2H3Cl. I guess that would just be VC – because it forms arbitrarily long chains, it’s written as (C2H3Cl)n. What a tradeoff – chemists making their lives easier at our expense. If we had to parse something like It seems like we can treat a group of parentheses as a subexpression on its own, multiplying every number by the coefficient that comes after it. You can look at this notation as a form of run-length encoding, where you express repeating substructures of data as pairs (s, n). It’s the simplest form of data compression, and it’s easy to encode an arbitrary string.