Saturday, March 21, 2009

Digest tag population

In comp.lang.lisp, Ken Tilton relayed a fun exercise involving a two-tiered list of pet populations, selections, and an unusual sort order. He proposed it as a test of language mastery because of his requirement to write "in one go" a single-function solution.

This style of development would be highly unusual with Lisp. Having the whole language available at an interactive read-eval-print loop promotes an incremental, bottom-up approach. As Paul Graham explains, bottom-up design in Lisp is more than building up a library: experienced programmers modify the language itself to make expressing the problem more straightforward.

I wrote a solution in Haskell: Aspects need improvement. The name of the type-synonym PetTags is plural, which is often better expressed as a list type, e.g., [PetTag]. The sort comparison functions (used on lines 39 and 40) are inconsistent in expression. The definition feels clunky and verbose.

In comp.lang.haskell, Florian Kreidler made my code much more elegant:

A more natural Haskell development style would be writing a function, checking it for correctness, and repeating in tiny increments. In the code below, I first wrote flatten, then I wrote select to extract the desired animals, followed by largest to extract the top n by population, and finally I wove them together to create digestTagPopulation.

(Github has a feature request for embedding particular revisions of gists. That would have come in handy in this post.)

No comments: