The Joy of Program Chrestomathy

After days spent wrestling with an intractable mess of code, with the winter rain beating on the windows, I sometimes find it difficult to maintain my keenness for development work. Is it like this for everyone? You read of cool startups where the California sun streams through the windows and everyone is young and cool, pausing only to make clever changes to open-source projects on GitHub. They plough through life-enhancing programming tasks, doing continuous delivery, group-hugs with DevOps comrades, and other Agile heroics. Sigh.

Of course, even the cynical developer and DBA has guilty pleasures: short breaks from the main task that serve to clear the brain and get the mental juices flowing again.  We get our kicks from forums, scoring points on StackOverflow, answering questions on ASK SQLServerCentral and so on, maybe even writing blogs or articles. Finally, let’s not forget program chrestomathy.

A program chrestomathy is a collection of similar programs written in various programming languages, for the purpose of demonstrating differences in syntax, semantics and idioms for each language. Several sites collect program chrestomathies, but I only recently came to appreciate the pleasures of the Rosetta Code Wiki site whilst looking for a good algorithm for calculating Levenshtein distance. I can recommend it. There seems no better way of learning about other programming languages than through comparing the way they tackle common algorithms, puzzles and chores. There are over three hundred different languages represented on the Wiki. To my delight, I found a programming language called Factor, but there was also wonderful, old, hoary Fortran, Ada or Algol code, exotic stuff like Haskell, Lua, M4 or OCaml, and all the mainstream languages such as C#, Java, Javascript or VB. Dialects of C abounded, such C++, as C++/CLI, UC++, Feature C++, Clik++, OpenC++ and Objective C. How great for flexing flabby programming muscles, to rise to the challenge of coding to one of the many tasks that they feature.

Outside a tech interview, you might never need to care about an algorithm, or brainteaser, but here there are over 700 of them. Even so, whilst exploring the site I found many tasks for which TSQL or PowerShell were well suited, but that weren’t there, yet!

When browsing the site, I tend to have mixed emotions. Occasionally I find a task over which I’d labored, imagining myself a pioneer, only to find almost identical algorithms already worked out, sometimes, irritatingly, in Cobol or Algol.  Was everything invented thirty or more years ago? I think that program chrestomathy may be growing on me as a furtive pursuit when the February blues get too much.

One of the tasks that I noticed was a routine to take an integer and turn it into a English cardinal number. So 1 becomes ‘One’, -413 becomes ‘minus four hundred and thirteen’ and so on. Some solutions were good, others pathetic, but all amusing.

Here is my TSQL solution to the task. Sure it is going to be slow but it does the job.