1.6 The Interactions Window
The interactions window lets you type an expression after the > prompt for immediate evaluation. You cannot modify any text before the last > prompt. To enter an expression, the blinking caret must appear after the last prompt, and also after the space following the prompt.
When you type a complete expression and hit Enter or Return, DrRacket evaluates the expression and prints the result. After printing the result, DrRacket creates a new prompt for another expression. Some expressions return a special “void” value; DrRacket never prints void, but instead produces a new prompt immediately.
If the expression following the current prompt is incomplete, then DrRacket will not try to evaluate it. In that case, hitting Enter or Return produces a new, auto-indented line. You can force DrRacket to evaluate the expression by typing Alt-Return or Command-Return (depending on your platform).
To copy the previous expression to the current prompt, type ESC-p (i.e., type Escape and then type p). Type ESC-p multiple times to cycle back through old expressions. Type ESC-n to cycle forward through old expressions.
Clicking the Run button evaluates the program in the definitions window and makes the program’s definitions available in the interactions window. Clicking Run also resets the interactions window, erasing all old interactions and removing old definitions from the interaction environment. Although Run erases old > prompts, ESC-p and ESC-n can still retrieve old expressions.
1.6.1 Errors
Whenever DrRacket encounters an error while evaluating an expression, it prints an error message in the interactions window and highlights the expression that triggered the error. The highlighted expression might be in the definitions window, or it might be after an old prompt in the interactions window.
For certain kinds of errors, DrRacket turns a portion of the error message into a hyperlink. Click the hyperlink to get help regarding a function or keyword related to the error.
For some run-time errors, DrRacket shows a bug icon next to the error message. Click the bug icon to open a window that shows a “stack” of expressions that were being evaluated at the time of the error. In addition, if the expressions in the stack appear in the definitions window, a red arrow is drawn to each expression from the next deeper one in the stack.
1.6.2 Input and Output
Many Racket programs avoid explicit input and output operations, obtaining input via direct function calls in the interactions window, and producing output by returning values. Other Racket programs explicitly print output for the user during evaluation using write or display, or explicitly request input from the user using read or read-char.
Explicit input and output appear in the interactions window, but within special boxes that separate explicit I/O from normal expressions and results. For example, evaluating
> (read)
in the interactions window produces a special box for entering input:
Type a number into the box and hit Enter, and that number becomes the result of the (read) expression. Once text is submitted for an input box, it is moved outside the input box, and when DrRacket shows a new prompt, it hides the interaction box. Thus, if you type 5 in the above input box and hit Return, the result appears as follows:
> (read) |
5 |
5 |
> _ |
In this case, the first 5 is the input, and the second 5 is the result of the (read) expression. The second 5 is colored blue, as usual for a result printed by DrRacket. (The underscore indicates the location of the blinking caret.)
Output goes into the interactions window directly. If you run the program
#lang racket |
(define v (read)) |
(display v) (newline) |
v |
and provide the input S-expression (1 2), the interactions window ultimately appears as follows:
(1 2) |
(1 2) |
(1 2) |
> _ |
In this example, display produces output immediately beneath the input you typed, and the final result is printed last. The displayed output is drawn in purple. (The above example assumes default printing. With constructor-style value printing, the final before the prompt would be (list 1 2).)
Entering the same program line-by-line in the interactions window produces a different-looking result:
> (define v (read)) |
(1 2) |
> (display v) |
(1 2) |
> v |
(1 2) |
> _ |
Depending on the input operation, you may enter more text into an input box than is consumed. In that case, the leftover text remains in the input stream for later reads. For example, in the following interaction, two values are provided in response to the first (read), so the second value is returned immediately for the second (read):
> (read) |
5 6 |
5 |
> (read) |
6 |
> _ |
The following example illustrates that submitting input with Return inserts a newline character into the input stream:
> (read) |
5 |
5 |
> (read-char) |
#\newline |
> _ |
Within a #lang racket module, the results of top-level expression print the same as the results of an expression entered in the interactions window. The reason is that #lang racket explicitly prints the results of top-level expressions using (current-print), and DrRacket sets (current-print) to print values in the same way as for interactions.