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 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.
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.
(exit)
at the REPL, except that it works
even if Scheme is running an evaluation, and does not request
confirmation.
(quit)
at the REPL, except that it works
even if Scheme is running an evaluation.
(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.
(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.
(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.
(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.
Another way of exiting a REPL is to use the restart
procedure:
;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 ]=>
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:
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.
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.