Home

Languages with Alternative "Hello World" Programs

The classic "Hello World" program, a program that simply prints the string "Hello World" to the console, is used in many languages as a standard example of a minimal program. Some programming languages have powerful features that can be showcased in an equally minimal program, yielding an example program that is much more interesting than the standard "Hello World". The examples that follow are not necessarily as standard as "Hello World" in other languages (i.e. other users might feel there is a more appropriate alternative) but I have seen them frequently enough to consider them quasi-standard at least and I use them when introducing people to the languages.

APL

The program:

        (+⌿÷≢)
    

What it does: Calculates the sum (+⌿) and divides (÷) by the count (). In other words, calculates the average. There are slight variations on this program depending on what style of APL one is writing (this is called a tacit program because there are no named variables). This program will look similar in related languages like J and K.

Why it's a good "Hello World" alternative: This program showcases APL's ability to operate over vectors (i.e. lists of things) in an extremely terse manner as well as the composibility of its symbols.

Learn More: tryapl.org

Prolog

The program[1]:

        member(X, [X|_]).
        member(X, [_|Y]) :- member(X, Y).
    

What it does: The first line is called a fact and is read "X is a member of the head of the list." The second line is called a rule and is read "X is a member of the list if X is a member of the tail of the list." In other words, we have defined a predicate member which relates an element to a list and evaluates to true if the element is in the list and false otherwise.

Why it's a good "Hello World" alternative: Prolog is extremely expressive when dealing with recursive relations. If your problem can be represented with a tree structure, then an elegant solution can probably be found in Prolog. Additionally, this program showcases Prolog's pattern matching capabilities. When running the code, one will find that the predicate can be evaluated in ways that are unusual for programmers coming from imperative languages:

Learn More: The Power of Prolog

Forth

The program:

        : square dup * ;
    

What it does: Defines a word (the Forth term for a function) called "square" which duplicates the top of the stack and multiplies the top two elements of the stack together.

Why it's a good "Hello World" alternative: Forth is all about defining small words and composing them into larger words until you have solved a given problem (and no more). This program showcases how easy it is to define new words in Forth (note the lack of syntactic ceremony present in other languages) as well as its stack based evaluation.

Learn More: Starting Forth

6502 Assembly (Commodore 64)

The program:

        loop:
        INC $D020
        JMP loop
    

What it does: Creates an infinite loop that increments the color value for the screen border on a Commodore 64 computer. This causes the border to rapidly cycle through all possible colors, creating a strobe effect.

Why it's a good "Hello World" alternative: This one is more of a personal standard, others will probably have their own preferences (especially those with photosensitivity). It's much simpler than actually printing text to the screen, and also provides visually impressive visual feedback with a brevity that is impossible on modern computers. This was also the first code I tested with my 6502 simulator, which itself was written in 6502 assembly on the Commodore 64.

Learn More: Machine Language for the Commodore 64, 128, and Other Commodore Computers by Jim Butterfield


1. Code and description from page 50 of the book Programming in Prolog, 4th Edition by W. F. Clocksin