The Main Loop
In any interactive session,
Mathematica effectively operates in a loop. It waits for your input, processes the input, prints the result, then goes back to waiting for input again. As part of this "main loop",
Mathematica maintains and uses various global objects. You will often find it useful to work with these objects.
You should realize, however, that if you use
Mathematica through a special front end, your front end may set up its own main loop, and what is said here may not apply.
| In[n] | the expression on the nth input line |
| InString[n] | the textual form of the nth input line |
| %n or Out[n] | the expression on the nth output line |
| Out[{n1,n2,...}] | a list of output expressions |
| %%...% (n times) or Out[-n] | the expression on the nth previous output line |
| MessageList[n] | a list of messages produced while processing the nth line |
| $Line | the current line number (resettable) |
Input and output expressions.
In a standard interactive session, there is a sequence of input and output lines.
Mathematica stores the values of the expressions on these lines in
In[n] and
Out[n].
As indicated by the usual
In[n]:= prompt, the input expressions are stored with delayed assignments. This means that whenever you ask for
In[n], the input expression will always be reevaluated in your current environment.
This assigns a value to x.
| Out[1]= |  |
|
Now the value for x is used.
| Out[2]= |  |
|
This removes the value assigned to x. |
This is reevaluated in your current environment, where there is no value assigned to x.
| Out[4]= |  |
|
This gives the textual form of the second input line, appropriate for editing or other textual manipulation.
Out[5]//InputForm= |
| |  |
|
| $HistoryLength | the number of previous lines of input and output to keep |
Specifying the length of session history to keep.
Mathematica by default stores
all your input and output lines for the duration of the session. In a very long session, this may take up a large amount of computer memory. You can nevertheless get rid of the input and output lines by explicitly clearing the values of
In and
Out, using
Unprotect[In, Out], followed by
Clear[In, Out]. You can also tell
Mathematica to keep only a limited number of lines of history by setting the global variable
$HistoryLength.
Note that at any point in a session, you can reset the line number counter
$Line, so that, for example, new lines are numbered so as to overwrite previous ones.
| $PreRead | a function applied to each input string before being fed to Mathematica |
| $Pre | a function applied to each input expression before evaluation |
| $Post | a function applied to each expression after evaluation |
| $PrePrint | a function applied after Out[n] is assigned, but before the result is printed |
| $SyntaxHandler | a function applied to any input line that yields a syntax error |
Global functions used in the main loop.
Mathematica provides a variety of "hooks" that allow you to insert functions to be applied to expressions at various stages in the main loop. Thus, for example, any function you assign as the value of the global variable
$Pre will automatically be applied before evaluation to any expression you give as input.
For a particular input line, the standard main loop begins by getting a text string of input. Particularly if you need to deal with special characters, you may want to modify this text string before it is further processed by
Mathematica. You can do this by assigning a function as the value of the global variable
$PreRead. This function will be applied to the text string, and the result will be used as the actual input string for the particular input line.
This tells Mathematica to replace listHead by {...} in every input string.
| Out[6]= |  |
|
You can now enter lists as listHead expressions.
| Out[7]= |  |
|
You can remove the value for $PreRead like this, at least so long as your definition for $PreRead does not modify this very input string. |
Once
Mathematica has successfully read an input expression, it then evaluates this expression. Before doing the evaluation,
Mathematica applies any function you have specified as the value of
$Pre, and after the evaluation, it applies any function specified as the value of
$Post. Note that unless the
$Pre function holds its arguments unevaluated, the function will have exactly the same effect as
$Post.
$Post allows you to specify arbitrary "postprocessing" to be done on results obtained from
Mathematica. Thus, for example, to make
Mathematica get a numerical approximation to every result it generates, all you need do is to set
$Post=N.
This tells Mathematica to apply N to every result it generates.
| Out[9]= |  |
|
Now Mathematica gets a numerical approximation to anything you type in.
| Out[10]= |  |
|
This removes the postprocessing function you specified. |
As soon as
Mathematica has generated a result, and applied any
$Post function you have specified, it takes the result, and assigns it as the value of
Out[$Line]. The next step is for
Mathematica to print the result. However, before doing this, it applies any function you have specified as the value of
$PrePrint.
This tells Mathematica to shorten all output to two lines. |
Only a two-line version of the output is now shown.
| Out[13]= |  |
|
This removes the value you assigned to $PrePrint. |
There are various kinds of output generated in a typical
Mathematica session. In general, each kind of output is sent to a definite
output channel, as discussed in
"Streams and Low-Level Input and Output". Associated with each output channel, there is a global variable which gives a list of the output streams to be included in that output channel.
Output channels in a standard Mathematica session.
By modifying the list of streams in a given output channel, you can redirect or copy particular kinds of
Mathematica output. Thus, for example, by opening an output stream to a file, and including that stream in the
$Echo list, you can get each piece of input you give to
Mathematica saved in a file.
| Streams[] | list of all open streams |
| Streams["name"] | list of all open streams with the specified name |
| $Input | the name of the current input stream |
Open streams in a Mathematica session.
The function
Streams shows you all the input, output and other streams that are open at a particular point in a
Mathematica session. The variable
$Input gives the name of the current stream from which
Mathematica input is being taken at a particular point.
$Input is reset, for example, during the execution of a
Get command.
| $MessagePrePrint | a function to be applied to expressions that are given in messages |
| $Language | list of default languages to use for messages |
Parameters for messages.
There are various global parameters which determine the form of messages generated by
Mathematica.
As discussed in
"Messages", typical messages include a sequence of expressions which are combined with the text of the message through
StringForm.
$MessagePrePrint gives a function to be applied to the expressions before they are printed. The default for
$MessagePrePrint uses
Short for text formatting and a combination of
Short and
Shallow for typesetting.
As discussed in
"International Messages",
Mathematica allows you to specify the language in which you want messages to be produced. In a particular
Mathematica session, you can assign a list of language names as the value of
$Language.
| Exit[] or Quit[] | terminate your Mathematica session |
| $Epilog | a global variable to be evaluated before termination |
Terminating Mathematica sessions.
Mathematica will continue in its main loop until you explicitly tell it to exit. Most
Mathematica interfaces provide special ways to do this. Nevertheless, you can always do it by explicitly calling
Exit or
Quit.
Mathematica allows you to give a value to the global variable
$Epilog to specify operations to perform just before
Mathematica actually exits. In this way, you can for example make
Mathematica always save certain objects before exiting.
| $IgnoreEOF | whether to ignore the end-of-file character |
A global variable that determines the treatment of end-of-file characters.
As discussed in
"Special Characters: Strings and Characters",
Mathematica usually does not treat special characters in a special way. There is one potential exception, however. With the default setting
$IgnoreEOF=False,
Mathematica recognizes end-of-file characters. If
Mathematica receives an end-of-file character as the only thing on a particular input line in a standard interactive
Mathematica session, then it will exit the session.
Exactly how you enter an end-of-file character depends on the computer system you are using. Under Unix, for example, you typically press
Ctrl+D.
Note that if you use
Mathematica in a "batch mode", with all its input coming from a file, then it will automatically exit when it reaches the end of the file, regardless of the value of
$IgnoreEOF.