;The first program (begin (display "Hello, World!"") (newline))
begin-form is Scheme’s way of
introducing a sequence of subforms. In this
case there are two subforms. The first is a call to
display procedure that outputs its argument
"Hello, World!"") to the console (or
“standard output”). It is followed by a
procedure call, which outputs a carriage return.
To run this program, first start your Scheme. This is usually done by typing the name of your Scheme executable at the operating-system command line. E.g., in the case of MzScheme , you type
at the operating-system prompt.
This invokes the Scheme listener, which reads
your input, evaluates it, prints the result (if
any), and then waits for more input from you. For this
reason, it is often called the read-eval-print loop.
Note that this is not much different from your
operating-system command line, which also reads your
commands, executes them, and then waits for more. Like the
operating system, the Scheme listener has its own prompt —
usually this is
>, but could be something else.
At the listener prompt, load the file
hello.scm. This is done by typing
Scheme will now execute the contents of
Hello, World! followed
by a carriage return. After this, you will get the listener
prompt again, waiting for more input from you.
Since you have such an eager listener, you need not always write your programs in a file and load them. Sometimes, it is easier, especially when you are in an exploring mood, to simply type expressions directly at the listener prompt and see what happens. For example, typing the form
(begin (display "Hello, World!"") (newline))
at the Scheme prompt produces
Actually, you could simply have typed the form
"Hello, World!"" at the listener, and you would have
obtained as result the string
because that is the result of the listener evaluating
Other than the fact that the second approach produces a
result with double-quotes around it, there is one other
significant difference between the last two programs.
The first (i.e., the one with the
begin) does not evaluate
to anything — the
Hello, World! it emits is a
side-effect produced by the
newline procedures writing to the standard output.
In the second program, the form
"Hello, World!"" evaluates to the result, which
in this case is the same string as the form.
Henceforth, we will use the notation
=> to denote
E => v
indicates that the form
E evaluates to a result value
(begin (display "Hello, World!"") (newline)) =>
(i.e., nothing or void), although it has the side-effect of writing
to the standard output. On the other hand,
"Hello, World!"" => "Hello, World!""
In either case, we are still at the listener. To exit, type
and this will land you back at the operating-system command-line (which, as we’ve seen, is also a kind of listener).
The listener is convenient for interactive testing of programs and program fragments. However it is by no means necessary. You may certainly stick to the tradition of creating programs in their entirety in files, and having Scheme execute them without any explicit “listening”. In MzScheme, for instance, you could say (at the operating-system prompt)
mzscheme -r hello.scm
and this will produce the greeting without making you
the listener. After the greeting,
return you to the
operating-system prompt. This is almost as if you said
echo Hello, World!
You could even make
hello.scm seem like an
operating-system command (a shell script or a
batch file), but that will have to wait till