Poor Man's Time Machine: Lazy Evaluation in JavaScript and Haskell

2026-06-08

Link: https://irfanali.org/blog/repmin

HN Discussion: 1 points, 0 comments

The URL points to a writeup on repmin — a famously deceptive little programming puzzle that has been a favorite of functional programming educators for decades. The problem statement sounds trivial: given a tree of numbers, return a tree of the same shape where every leaf is replaced by the minimum value from the original tree. The catch is the elegance constraint — can you do it in a single traversal?

In an eager language, the obvious answer is no. You need one pass to find the minimum and a second pass to rebuild the tree. But in Haskell, Richard Bird showed in 1984 that lazy evaluation lets you write something that looks impossibly circular: the function returns both the minimum and the rebuilt tree in one go, with the rebuilt tree referring to the minimum that the same call is still computing. It feels like time travel — hence the post's title.

What makes this particular writeup interesting is the JavaScript angle. JavaScript isn't lazy by default, but it has all the primitives needed to simulate laziness: thunks, closures, generators, and now `Promise`-based deferred evaluation. Translating repmin to JS forces you to make explicit what Haskell hides — and that's where the educational value lives. You see exactly which knots tying-the-knot actually ties.

Why a technical audience should care:

The post has one upvote and zero comments, which is a shame — this is exactly the kind of small, dense programming essay HN used to surface routinely. It's the genre of "I learned a beautiful trick, here's why it matters," not chasing trends, not selling a product.

Why it deserves more upvotes: A classic functional programming gem (Bird's repmin) translated into JavaScript — the kind of evergreen, mind-expanding technical writing that rewards careful reading far more than another framework launch.

All newsletters