A common pattern for introducing a programming language is the Hello World program.

But what exactly can you tell about a language from looking at its Hello World program?

public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); } }

What does that tell you about object-oriented programming using classes?

#include int main(int argc, char *argv[]) { printf("Hello, World!

"); return 0; }

What does that tell you about pointers?

module Main where main = putStrLn "Hello, World!"

What does that tell you about laziness and functional purity?

:- write('Hello, World!'), nl.

What does that tell you about nondeterminism and backtracking?

<?php echo "Hello, World!"; ?>

What does that tell you about poorly-designed languages and libraries? (PHP isn’t all bad. I spent years using PHP for most projects of my own free will. And there is a lot of evidence that it’s easier than many languages for helping novices create interesting things. As with many simple technologies, the problems appear when you start applying them to more complex projects.)

The answer is that none of these examples provides much indication of how other (even simple) programs can be written in them; nor do they demonstrate the language’s characteristic features or common idioms. When I saw a lot of Hello World programs in this quiz, I realised that the only feature to distinguish many of them was the name of the standard library function used to print a string. The features which make a language special were going unmentioned.

Benefits of Hello World

Hello World has two positive features:

In a tutorial for a new programming language, it provides a simple test: if you type in and run this short program, and see the output “Hello, World!”, then you’ve at least managed to install the compiler or interpreter correctly.

It is also a kind of Rosetta Stone. If you know how to write Hello World in one language, and you see a Hello World in another language, you can use the common patterns to (experimentally) understand the new ones. Here’s one:

hello: db 'Hello World', 13, 10, '$' mov dx, offset hello mov ah, 9h int 21h

But can you guess what mov and int mean in that language? (And what does this example tell you about the Intel 8086 instruction set?)

The reliability of this kind of approximate program translation is therefore quite limited.

But in regard to the first point, Hello World is still a useful — and short — piece of code for testing that a programming environment is ready for use.

Improving on Hello World

If Hello World is inadequate, what canonical program would better serve to demonstrate the salient features of a programming language?

Must it use variables? Must it have flow control? Must it actually compute something? Must it use non-primitive data structures? Must it exhibit use of the language’s code organisation features?

These are some of the more generic properties that programs can have.

The first problem is deciding which language features are fundamental to programming. A Perl programmer might suggest extracting values from strings, but a Logo programmer might say the obvious example is drawing geometric figures on the screen. All languages have peculiar strengths. The aim is not to choose a problem that shows off the abilities of our favourite language, but to show how the language can be used for the same tasks as other languages.

There are also constraints of brevity and simplicity. All general purpose programming languages allow compilers or translators to be written; a self-hosted compiler is a common benchmark of a language’s practicality. But (excepting, perhaps, in the case of LISP), a compiler or interpreter example in every programming language article on Wikipedia is untenable. Moreover, compilers (despite their importance to computing) are very abstruse compared to most other programs. Requiring even a basic knowledge of them for learning a new language is prohibitive.

So the second problem is to find a compromise between simplicity and variety of language features employed.

Possible alternatives

Examples of simpler programs from introductory programming are programs such as the factorial function, Fibonacci sequence, and Euclid’s GDC algorithm. These often demonstrate the first three features from the list above, but do not often touch on the remaining two.

Part of the reason is that they are simple numerical functions. One of the benefits of an example program that exhibits the fourth property would be to show that a computer does not just manipulate numbers. It can manipulate any kind of information as long as the structure and operations can be specified. Programs that perform simple string operations (such as reversing a string or counting words), arguably demonstrate this property.

Strings are built in to most languages (with the notable exception of C) and so are arguably primitive, too. Demonstration of user-defined structures is also desirable; common examples of these are collection types such as queues.

It may be harder to demonstrate the last property convincingly in a short program. Features that aid code organisation are most useful in large and complicated programs. So I will consider that an optional property; small programs have less scope for organisational efficiency.

One suggestion (from David Pearce) was Conway’s Game of Life: a simple simulation of finite automata. This seems like a good demonstration of most of the properties. It also has the advantage of being accessible: you don’t have to understand much theory to appreciate that patterns arising out of simple rules. It does have the potential downside of being difficult to get right, especially the task of updating a simulation based on its current state; but that provides an opportunity to show how a language helps with that issue.

What other possible worthy successors to Hello World are there? Comment below if you can suggest some!