Thursday, January 20, 2011

Fortran in any language

One of the witty remarks about programming goes: I can write Fortran in any language!

The comment is usually made during a discussion of programming languages, usually new languages. It is generally used to indicate the ability to limit the use of a new language to the features of an older language, such as using a new C++ compiler but writing the constructs of C. Since C++ is a superset of C, all C programs work. One can avoid the hard work of learning C++ and write the comfortable syntax of C while claiming to write C++ code. (One can make the claim, but savvy geeks will quickly learn the fraud.)

While witty, the comment is not quite true. And I think that it is a bit unfair that we keep picking on Fortran.

The idea, perhaps, is that Fortran is so basic, so rudimentary, so primitive, that every language has its features (plus a whole lot more that makes it a different language). Thus, BASIC is Fortran with better looping and input-output, and Pascal is Fortran with pointers and better structure, and one can write "primitive" Fortran-like programs in either BASIC or Pascal. The comment "I can write Fortran in any language" is a condensation of "I can write programs in any language that use a limited subset of that language which is very close to Fortran".

I think that this verbal abuse of Fortran is a bit undeserved. Fortran may be many things, and it may even be primitive, but it is not the parent object of modern programming languages, with descendants that have everything of Fortran and extra bits. Fortran had a lot that other languages abandoned.

For example, in FORTRAN (the early versions of the language: FORTRAN II and FORTRAN IV) had no variable declarations. Pascal and C (and Java and C#) require all variables to be declared in advance. Python and Ruby have gotten away from this, returning to the original FORTRAN style, and Perl, oddly, does not require declarations unless you use the 'strict' module which changes Perl to require them.

FORTRAN also had the GOTO statement, which was kept in C and even in C++ (in a reduced form). Pascal eliminated the GOTO, and it is not to be found in Java or other modern languages.

One interesting (and mind-bending) construct in FORTRAN was the arithmetic IF. Instead of the logical if (IF THEN ELSE ), FORTRAN used an arithmetical expression construct; IF LABEL1, LABEL2, LABEL3. Execution was routed to one of the labels based on the value of the expression: negative values to one label, positive values to another, and zero values to the third. Only with the introduction of FORTRAN-77 did we see the IF...THEN...ELSE construct.

FORTRAN has changed over the years. It started with a pretty good idea of a high-level language, and morphed as we figured out what we really wanted in a language. It's not C# or Ruby, and it won't be. But it has changed more than any other language (with the possible exception of Visual Basic).

Its resilience is a lesson to us all.

No comments: