TAOCP by Knuth are quite something to read

It’s been almost one year since I wrote my last blog post. I hope to write more frequently in the future on topics like in this post.

About one month ago I received my copy of The Art of Computer Programming, Volume 4A: Combinatorial Algorithms, Part 1 by Donald Knuth from Amazon. I wasn’t expecting to read it from cover to cover but I wanted to see what Knuth’s work look like because there are very hot discussions on the Internet about whether Knuth’s work are worth reading. Knuth himself also has joined one of such discussions.

The first thing I noticed when I opened the book was that some pages were really math-heavy. It was a frightening first contact. However when I tried to read some parts I saw that it was not that bad and most of the stuff was explained in plain English and most mathematical stuff belong to proofs of given theorems. On the other hand the volume I got was particularly interesting for me because it’s about a useful, fun and challenging topic, Combinatorial Algorithms which Knuth himself also states as “The kind of programming I like best”.

A few days after I got the book (what a coincidence!) I needed an algorithm for finding combinations (of m) of integers whose sum was equal to a given number n. The algorithm I needed was Integer Partitioning which I was able to discover thanks to the extended index at the end of the book (and Google of course). I just put the book nearby and implemented the algorithm in Java even without understanding it completely; it was a quite straightforward process.

I am now a happier developer as I have been able to utilize Knuth’s work for a real world case :-)

I may write more about the algorithm-to-Java transformation process later as it was also an interesting experience.

Finally, greetings to my colleague, Derya Susman who shared this joyful experience with me ;-)

Case Mapping for Turkish in Haskell

Turkish is generally challenging for i18n related software particularly because of its special case for the I, ı, İ, i letters. The problem is discussed in detail here.

Haskell has recently got complete support for various Unicode services through text and text-icu packages. Some recent updates to text and text-icu are explained in the author’s blog.

The following is a Haskell code snippet which shows an example of case mapping for Turkish using the mentioned packages.

1
2
3
4
5
6
7
8
import Data.Text (pack, unpack)
import Data.Text.ICU (LocaleName(Locale), toLower)

main = do
  let trLocale = Locale "tr-TR"
  let upStr = "ÇIİĞÖŞÜ"
  let lowStr = unpack $ toLower trLocale $ pack upStr
  putStrLn $ "toLower " ++ upStr ++ " gives " ++ lowStr

The program simply outputs (as expected):

1
toLower ÇIİĞÖŞÜ gives çıiğöşü

Perhaps one thing that needs to be explained regarding this code is that pack and unpack are used there for converting from String to Text and vise-versa as required by the packages we use.

Although the code is a little bit imperative (well, I am new to Haskell ;-) ) it should give an idea of how to use the functionality for Turkish or any other language or locale.