2012-02-27 Haskell, Python, and Readability

This weekend on /r/programming someone posted a nice introduction to tries using Python. A while ago, I had implemented a mini web server to do completion and correction of words using Ternary Search Tries in Haskell, and since the trie post generated a lot of interest I decided to post mine too.

Then, someone else posted a blog article commenting on the readability of Python and Haskell based on my web server code and the trie example, concluding that the Python version was much more readable.

I personally prefer writing and reading Haskell, but I think that the comparison is not fair since the data structures compared are very different. Thus, I quickly coded an Haskell version of the code in the original blog post, so that you can compare code that does, more or less, the same thing.

The only difference is that I don’t store the current word in each node, since it is not necessary—the current word can be kept track of when traversing the path. Also, I’m using lists instead of sets when returning the completions, but changing that would be trivial using Data.Set.

Moreover, the Haskell version has the advantage of working with any list, so for example it would work with list of integers as well as Strings. This could be further abstracted to “unconsable” data types (ListLike offers that and more) but that’s not that relevant here.

So, here’s the original Python code:

And this is the Haskell version:

I didn’t read the Python version too carefully so if I missed something let me know!

You can discuss this post on reddit.


Update: after the reddit discussion, I made the variable names a bit clearer.