Search Index in 150 Lines of Haskell

This is not mine, but I thought it was very interesting:

https://entropicthoughts.com/search-index-150-lines-haskell

I stumbled over Bart de Goede’s article on building a full-text search engine in 150 lines of Python, and was reminded of my quest to show how useful Haskell is for solving real-world problems. Python is an eminently practical language, so nobody is surprised this can be done in Python. But Haskell? The Python code spends a lot of time updating mutable dictionaries. Surely we cannot easily port this code over to Haskell.

Let’s find out.

9 Likes

Python can trivially be ported to Haskell via let and where clauses, with recursion substituting for loops. Hell, with increasing maturity of inline-python: Python interpreter embedded into haskell. , you can directly call Python libs as needed.

Mutation can be handled either by using equational reasoning to render the entire algorithm immutable, using higher-order functions or accumulating parameter loops to encode mutation.

What you get in return is:
Equational reasoning allows refactoring of algorithms to more efficient and readable forms
Greater and more expressive type safety by default
At least a 2x improvement in performance in most cases.

2 Likes