Ran into a rather interesting programming problem at work today that laziness provides a cool solution to! Given columnar data where data may be missing, number all non-missing entries vertically by column. Let me know how you'd do it, I'll share a fun solution below.

So the tricky part is that we don't know what number to start the second column at until we've processed the first column, and we can't just count the length of the column since some entries are missing.

A reasonable approach here would be to unzip, process the first column to count and format the entries, then use the count to process the second column, then zip them back up to get the final format, unfortunately in my actual problem this wasn't an option.

I had multiple data-sets to print and the numbers needed to be contiguous (i.e. the first column's numbering needs to count up across all N data-sets before continuing in column 2). So in general I wouldn't recommend the following approach unless you have similar constraints.

But anyways, here's how laziness in Haskell provides a novel solution for this, we can use a state monad with 2 distinct counts, one for each column, but use a fixed point to use the final result of the first column as the starting value for the second column!

Here's what it looks like, notice how `totalInFirstColumn` is used in both the lhs and rhs of the binding! This also allows us to process the data in a single pass, and also allowed me to to process multiple data-sets and have the numbering work out contiguously.

I normally prefer to avoid fixed points when possible, but the constraints in this case made it more composable and easier to read this way. I added a few comments of course.

If you like this sort of thing, check out Quchen's awesome article on loeb functions!

##### Follow us on Twitter

to be informed of the latest developments and updates!

Follow @tivitikothreadYou can easily use to @tivitikothread bot for create more readable thread!