Up Next

The canonical first program is the one that says "Hello, World!" on the console. Using your favourite editor, create a file called hello.scm with the following contents:

;The first program

(begin
  (display "Hello, World!")
  (newline))

The first line is a comment. When Scheme sees a semicolon, it ignores it and all the following text on the line.

The 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 the display procedure that outputs its argument (the string "Hello, World!") to the console (or ``standard output''). It is followed by a newline 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. Eg, in the case of MzScheme, you type

mzscheme

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

(load "hello.scm")

Scheme will now execute the contents of hello.scm, outputting 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

Hello, World!

Actually, you could simply have typed the form "Hello, World!" at the listener, and you would have obtained as result the string

"Hello, World!"

because that is the result of the listener evaluating "Hello, World!".

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 (ie, the one with the begin) does not evaluate to anything -- the Hello, World! it emits is a side-effect produced by the display and 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 evaluation. Thus

E => v

indicates that the form E evaluates to a result value of v. Eg,

(begin
  (display "Hello, World!")
  (newline))
=>

(ie, nothing or void), although it has the side-effect of writing

Hello, World!

to the standard output. On the other hand,

"Hello, World!"
=> "Hello, World!"

In either case, we are still at the listener. To exit, type

(exit)

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 deal with the listener. After the greeting, mzscheme will 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 chapter 16.

Up Next

Log in or register to write something here or to contact authors.