name
2009-02-05
Introduction
This program asks the user to enter their name, and then prints out a greeting. It demonstrates reading a line from standard input and writing a mixture of hard-coded and user-supplied values to standard output.
Ada
The program can be implemented in Ada as follows:
with Ada.Text_IO; use Ada.Text_IO;
procedure Name is
begin
Put("Enter your name: ");
declare
Name : String := Get_Line;
begin
Put("Hello, " & Name & "!");
end;
end Name;
The program uses Put
and
Get_Line
from Ada.Text_IO
. The name is
stored in a variable called Name
, which is declared in a
declare
block. The greeting is then printed out using
three subsequent calls to Put
.
The reason that the variable Name
is declared in a
declare block, rather than in the declarations part of the procedure
Name
, is that the length of the string must be known
at declaration time. Since the length of Name
is
not known until the line to store in it has been read, it cannot be
declared earlier than it is read. And it cannot be read earlier,
because then the program would wait for user input before having prompted
the user to enter anything.
Note that Get_Line
is available in Ada 2005, but not
in earlier versions. Some implementations emit a warning about the use
of the function, and older implementations will not be able to compile
the program at all.
The program contains 28 words.
C
In C, the program is as follows:
#include <stdio.h>
int main(int argc, char *argv[]) {
char name[64];
fputs("Input your name: ", stdout);
fgets(name, sizeof(name), stdin);
printf("Hello, %s!\n", name);
return 0;
}
The first thing worth noting is the declaration of
name. C doesn't have a dedicadet string type; instead, arrays of
characters are used, with the last character set to 0
(ASCII NUL
) to mark the end of the string.
char name[64];
declares an array of 64 characters, which we
hope will be enough to hold the name that will be entered.
For reading the name, we use fgets
, which we need to
supply the array it should store the name in, the size of that array,
and the stream to read the name from; in this case, standard input.
fgets
reads from the supplied stream until at most one
fewer than the supplied number of bytes have been read, the end of the
stream is reached, or the last character it read was a newline. The
characters it read are stored in the array, followed by a
0
.
The greeting is printed out using printf
, which takes a
format string as its first argument, and a number of other arguments
depending on the contents of the format string. Any character in the
format string is literally printed, except %
, which
introduces a formatting directive. The formatting directive
%s
takes an argument from the argument list and prints it
as if it were a string. \n
is how you put a newline inside
a string in C.
The program contains 24 words.
OCaml
In OCaml, the program looks as follows:
open Printf;;
print_string "Input your name: ";;
let name = read_line () in
printf "Hello, %s!\n" name
This program makes use of the Printf module, which,
among other things, defines the function printf
, which acts
very much like printf
in C.
The program contains 17 words.
Ruby
Here's the Ruby version of the program:
print 'Enter your name: '
name = gets.chomp
puts "Hello, #{name}!"
Note the usage of print
for printing out
the prompt without following it by a newline, and puts
to
print out the greeting, followed by a newline. gets
is used
to read the name. Calling chomp
on a string returns that
string, but without a trailing newline, if there is one. Note that
gets.chomp
means call
. Finally, we print out the
greeting. Note that gets
, and call
chomp
on the result#{…}
inside a double-quoted
string evaluates …
and places the result inside the
string.
The program contains 11 words.
Common Lisp
In Common Lisp, the program can be written like this:
(format t "Input your name: ")
(let ((name (read-line)))
(format t "Hello, ~A!~%" name))
The let
form is used to bind local
variables. Its syntax can be a bit mystifying at first, but is
actually really simple:
(let (binding …) form …)
where binding …
indicates one or
more bindings, and …
indicates one or more forms.
Each binding takes one of two forms: name
or (name
value)
. In the first case, name
is bound to
nil
, in the second case, it is bound to value
.
After all bindings are created, the forms are executed (and the value
returned by the last form is returned from the let
form.
In the program above, name
is bound to the value
returned by (read-line)
, which reads one line from standard
input and returns it (without a trailing newline). This is then printed
by format
, as per the format directive ~A
,
which takes the next argument to format
and prints it in
aestetic (that is, human-friendly) form.
There are 14 words in this program.