Go to the first, previous, next, last section, table of contents.

The Read-Eval-Print Loop

When you first start up Scheme from the command line (i.e not under Edwin), you will be typing at a program called the Read-Eval-Print Loop (abbreviated REPL). It displays a prompt at the left hand side of the screen whenever it is waiting for input. You then type an expression (terminating it with RET). Scheme evaluates the expression, prints the result, and gives you another prompt.

The Prompt and Level Number

The REPL prompt normally has the form

1 ]=>

The `1' in the prompt is a level number, which is always a positive integer. This number is incremented under certain circumstances, the most common being an error. For example, here is what you will see if you type f o o RET after starting Scheme:

;Unbound variable: foo
;To continue, call RESTART with an option number:
; (RESTART 3) => Specify a value to use instead of foo.
; (RESTART 2) => Define foo to a given value.
; (RESTART 1) => Return to read-eval-print level 1.

2 error> 

In this case, the level number has been incremented to `2', which indicates that a new REPL has been started (also the prompt string has been changed to remind you that the REPL was started because of an error). The `2' means that this new REPL is "over" the old one. The original REPL still exists, and is waiting for you to return to it, for example, by entering (restart 1). Furthermore, if an error occurs while you are in this REPL, yet another REPL will be started, and the level number will be increased to `3'. This can continue ad infinitum, but normally it is rare to use more than a few levels.

The normal way to get out of an error REPL and back to the top level REPL is to use the C-g interrupt. This is a single-keystroke command executed by holding down the CTRL key and pressing the G key. C-g always terminates whatever is running and returns you to the top level REPL immediately.

Note: The appearance of the `error>' prompt does not mean that Scheme is in some weird inconsistent state that you should avoid. It is merely a reminder that your program was in error: an illegal operation was attempted, but it was detected and avoided. Often the best way to find out what is in error is to do some poking around in the error REPL. If you abort out of it, the context of the error will be destroyed, and you may not be able to find out what happened.

Interrupting

Scheme has two interrupt keys under unix: C-g and C-c. Other systems, like the PC, may have more than two. The PC version has C-b, C-x, and C-u as well as C-g and C-c. The C-g key stops any Scheme evaluation that is running and returns you to the top level REPL. C-c prompts you for another character and performs some action based on that character. It is not necessary to type RET after C-g or C-c, nor is it needed after the character that C-c will ask you for.

Here are the more common options for C-c.

C-c i
Ignore the interrupt. Type this if you made a mistake and didn't really mean to type C-c.
C-c ?
Print help information. This will describe any other options not documented here.
C-c q
Similar to typing (exit) at the REPL, except that it works even if Scheme is running an evaluation, and does not request confirmation.
C-c z
Similar to typing (quit) at the REPL, except that it works even if Scheme is running an evaluation.
C-c C-c
Identical to typing C-g. If no evaluation is running, this is equivalent to evaluating
(cmdl-interrupt/abort-top-level)
The options C-c C-g and C-c g, supplied for compatibility with older implementations, are equivalent to C-c C-c.
C-c C-x
Abort whatever Scheme evaluation is currently running and return to the "current" REPL. If no evaluation is running, this is equivalent to evaluating
(cmdl-interrupt/abort-nearest)
The option C-c x, supplied for compatibility with older implementations, is equivalent to C-c C-x. On the PC version C-x is equivalent to C-c C-x.
C-c C-u
Abort whatever Scheme evaluation is running and go up one level. If you are already at level number 1, it just aborts the evaluation, leaving you at level 1. If no evaluation is running, this is equivalent to evaluating
(cmdl-interrupt/abort-previous)
The option C-c u, supplied for compatibility with older implementations, is equivalent to C-c C-u. On the PC version C-u is equivalent to C-c C-u.
C-c C-b
Suspend whatever Scheme evaluation is running and start a breakpoint REPL. The evaluation can be resumed by evaluating
(proceed)
in that REPL at any time. The option C-c b, supplied for compatibility with older implementations, is equivalent to C-c C-b. On the PC version C-b is equivalent to C-c C-b.

Restarting

Another way of exiting a REPL is to use the restart procedure:

procedure+: restart [K]
This procedure selects and invokes a restart method. The list of restart methods is different for each REPL; in the case of an error REPL, this list is printed when the REPL is started:

;Unbound variable: foo
;To continue, call RESTART with an option number:
; (RESTART 3) => Specify a value to use instead of foo.
; (RESTART 2) => Define foo to a given value.
; (RESTART 1) => Return to read-eval-print level 1.

2 error> 

If the k argument is given, it must be a positive integer index into the list (in this example it must be between one and three inclusive). The integer k selects an item from the list and invokes it. If k is not given, restart prints the list and prompts for the integer index:

2 error> (restart)
;Choose an option by number:
;  3: Specify a value to use instead of foo.
;  2: Define foo to a given value.
;  1: Return to read-eval-print level 1.

Option number:

The simplest restart methods just perform their actions. For example:

2 error> (restart 1)
;Abort!

1 ]=>

Other methods will prompt for more input before continuing:

2 error> (restart)
;Choose an option by number:
;  3: Specify a value to use instead of foo.
;  2: Define foo to a given value.
;  1: Return to read-eval-print level 1.

Option number: 3

Value to use instead of foo: '(a b)
;Value: (a b)

1 ]=>

The Current REPL Environment

Every REPL has a current environment, which is the place where expressions are evaluated and definitions are stored. When Scheme is started, this environment is the value of the variable user-initial-environment. There are a number of other environments in the system, for example system-global-environment, where the runtime system's bindings are stored.

You can get the current REPL environment by evaluating

(nearest-repl/environment)

There are several other ways to obtain environments. For example, if you have a procedure object, you can get a pointer to the environment in which it was closed by evaluating

(procedure-environment procedure)

Your programs create new environments whenever a procedure is called.

Here is the procedure that changes the REPL's environment:

procedure+: ge environment
Changes the current REPL environment to be environment (ge stands for "Goto Environment"). Environment is allowed to be a procedure as well as an environment object. If it is a procedure, then the closing environment of that procedure is used in its place.

procedure+: gst syntax-table
In addition to the current environment, each REPL maintains a current syntax table. The current syntax table tells the REPL which keywords are used to identify special forms (e.g. if, lambda). If you write macros, often you will want to make your own syntax table, in which case it is useful to be able to make that syntax table be the current one. gst allows you to do that.


Go to the first, previous, next, last section, table of contents.