As a preliminary exercise to jog my memory, here is the obligatory “Hello, world!” program in a variety of languages. On the one hand, the programs do nothing except output a string, and so don’t illustrate very much about the respective languages. On the other hand, the simplicity of the task does illustrate the difference between languages that allow you to begin coding right away versus those that require a considerable amount of setting up (importing libraries, declaring classes, etc.) before you even have a functioning program.
To be consistent, the programs will all output “Hello, world!”, followed by the newline character.
First up, of course, is C:
1 2 3 4 | #include<stdio.h> int main(int argc, char *argv[]) { printf("Hello, world!\n"); } |
C++ is similar, except that iostream is used instead of stdio:
1 2 3 4 5 | #include<iostream> using namespace std; int main() { cout << "Hello, world!" << endl; } |
Java requires that a class be defined:
1 2 3 4 5 | class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!\n"); } } |
C# looks like a mixture of C and Java:
1 2 3 4 5 6 | using System; public class HelloWorld { public static void Main() { Console.WriteLine("Hello, world!\n"); } } |
In an interview situation, I doubt that I would use C# or Java, unless explicitly asked. It wastes too much time to set up the classes. Also, when writing code on a whiteboard or on paper, the shallower your indentation level, the better, because you don’t want to use up space unnecessarily, and you’ll have to move things around when you’re asked to add to or modify the code.
In Perl, this program is just one line:
1 | print "Hello, world!\n"; |
The program is almost the same in Python:
1 | print "Hello, world!\n" |
The only difference is that the statement in Python does not need to end in a semicolon (although it is legal, it is superfluous). In Python 3 and above, print is a function:
1 | print("Hello, world!\n") |
In Ruby:
1 | puts "Hello, world!\n" |
In Common Lisp:
1 | (format t "Hello, world!~%") |
I’ll probably not be able to remember the format operators on the spot (e.g., “~%” instead of the much more common “\n” for “newline”).
In Scheme:
1 2 | (display "Hello, world!") (newline) |
In Prolog:
1 | write('Hello, world!'), nl. |
In Haskell:
1 | putStrLn("Hello, world!") |
Note that putStrLn already appends a newline character.
In Standard ML:
1 | print("Hello, world!\n"); |
In the Linux shell (e.g., bash):
1 | echo 'Hello, world!' |
The echo command outputs a trailing newline.
And now, for some mathematical languages. In Matlab:
1 | fprintf("Hello, world!\n"); |
In Octave (the above Matlab program will also work):
1 | printf("Hello, world!\n"); |
In Maple:
1 | print(`Hello, world!\n`); |
In Mathematica:
1 | Print["Hello, world!\n"] |
I suppose that, in the mathematical languages, rather than print “Hello, world!” to the display, a true “Hello, world!” program would assign that value to a string, and then evaluate the string (to itself).
In an interview situation, I’ll probably use C/C++, because that’s the general-purpose programming language that I have the most experience with. However, I think that Python is actually a better language for quickly mocking up working programs on the spot, so I’m going to practise using Python some more.
– davinci 11793

0 Responses to “Programming exercise: Hello, world!”