Aaron Fischer Ingenieur, Vater, Heimwerker, Problemlöser

02 August, 2009

Donald Knuth, der Meister der Lyrik

Programmiersprachen

Donald E. Knuth ist die lebende Legende im Softwareumfeld. Er widmet schon über Jahrzehnte hinweg sein Leben der Forschung und der formalen Beschreibung von Algorithmen. Sein still in progress Band Art of Computer Programming ist wahrlich ein Meisterwerk. Interessant ist aber, dass sich die wenigsten Informatiker an dieses monströse Werk wagen. Er ist auch der Auffassung, dass Programmcode mit derselben Sorgfalt verfasst werden sollten wie literarische Texte. Dokumentation und Code sollten vereint sein.

Let us change our traditional attitude to the construction of programs. Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. - Donald E. Knuth

Da ich auch einen gewissen Standard an Dokumentation und Programmcode habe und meinen Code ästhetisch ansprechend formatiere und schreibe (bzw. den Code so gestalte, dass er mich anspricht :)), bin ich natürlich immer wieder auf der Suche nach Vorbildern.

Deshalb dachte ich mir, wieso nicht mal den Quellcode von TeX (einem Textsatzsystem von Herrn Ph. D. Knuth persönlich) anschauen. Und tatsächlich: Im ersten Moment dachte ich, der Code sei nur die Dokumentation zu TeX. Er liest sich wie eine Definition oder ein Informatikbuch. Hier mal ein kleiner Auszug:

@ Now comes the harder part: At this point in the program, |cur_val| is a
nonnegative integer and $f/2^{16}$ is a nonnegative fraction less than 1;
we want to multiply the sum of these two quantities by the appropriate
factor, based on the specified units, in order to produce a |scaled|
result, and we want to do the calculation with fixed point arithmetic that
does not overflow.

@<<Scan units and set |cur_val| to $x\\cdot(|cur_val|+f/2^{16})$...@>>=
if inf then @<<Scan for \\(f)\\.{fil} units; |goto attach_fraction| if found@>>;
@<<Scan for \\(u)units that are internal dimensions;
  |goto attach_sign| with |cur_val| set if found@>>;
if mu then @<<Scan for \\(m)\\.{mu} units and |goto attach_fraction|@>>;
if scan_keyword("true") then @<<Adjust \\(f)for the magnification ratio@>>;
@.true@>>
if scan_keyword("pt") then goto attach_fraction; {the easy case}
@.pt@>>
@<<Scan for \\(a)all other units and adjust |cur_val| and |f| accordingly;
  |goto done| in the case of scaled points@>>;
attach_fraction: if cur_val>>=@"40000 then arith_error:=true
else cur_val:=cur_val*unity+f;
done:

Abgesehen von der gewöhnungsbedürftigen Syntax ist das fast schon Lyrik, was er da an den Tag legt. Leider sind nicht alle Programmiersprachen so biegsam, aber Ruby geht hier schon sehr in die richtige Richtung. Also hier ein Aufruf an alle: Doku + Code = Awesome! :)