3 min read

Advent of Code in Prolog, Haskell, Python and Scala

Here are some Advent of Code solutions:

Here are some comparative notes:

  • My Haskell solutions were mostly < 27 LoC. The Prolog solutions where considerably longer. The Prolog solutions were, on average, much harder to code for me.

  • My Prolog solutions ended up looking rather functional for the most part. I suspect this is because the harder puzzle tasks are heavily procedural. That is, there is often an element of simulating something, or a multi-stage calculation. As a result, it was easier to solve these puzzles in Haskell, since its more ergonomic for functional style programming.

  • I got hung up on using DCGs in Prolog which was a big time sink retrospectively. I used regular expressions in the Haskell version which made parsing inputs much quicker. I don’t think DCGs are great for small parsing tasks, they are much more difficult to troubleshoot than regular expressions and more verbose.

  • I think CHR rules were the most useful addendum to Prolog because they made the simpler simulations easy to write down declaratively.

  • Tabling, “impure” Prolog and clpfd are not guaranteed to be compatible. Some problems took ages to solve because I had to figure out why clpfd wasn’t doing what I expected.

  • Prolog didn’t make it easier to write search algorithms. It felt like it got in the way because of how awkward data structures like hash tables and priority queues feel in the logical paradigm.

  • I wrote the Python code mostly because I wondered how much of what I liked about Haskell I could preserve by just changing how I code in a more familiar language. Stylistically the results are very similar, and by problem 13 I was averaging just 14 LoC. In fact it felt much more ergonomic to code in “functional” Python, because I could also switch to a bit of imperative programming when it was the easier way forward.

  • I added Scala last because I needed to learn it for work. I’m not particular fond of JVM languages because they carry a lot of baggage, but Scala is very pleasant to code in. It feels familiar coming from Haskell.

  • Scala is reasonably terse, and other than having to declare the types of things from time to time, it feels a lot like a dynamic programming language. On average my like-for-like Scala solutions are about the same LoC as Python but typically have more characters.

  • Scala comes with a huge number of list oriented functions. It made some programs much easier to write.

  • My favourite language to code in was Prolog. It was the least productive and most difficult, but it was also the only mind expanding experience of the four, because of the backward way it forces you to think. Python was certainly the most productive language for me because it is the most familiar. Scala was the quickest language to learn: I was more or less productive with within a few hours because of the similarity to Haskell.