Although it is theoretically possible to write long Scheme programs by running Scheme in interactive mode and typing in the constituent definitions and expressions, it's more usual to develop them ``off-line'' using a text editor. On MathLAN, I strongly recommend the XEmacs editor, which you can invoke by clicking on the pencil-and-paper icon on the bottom row of the HP VUE front panel.
To start editing a Scheme program file, click on the icon showing a dog-eared sheet of paper, at the left end of the toolbar near the top of the XEmacs window. A prompt will appear at the bottom of the XEmacs window, asking what file you want to edit and making a guess at the directory in which the file is located:
Find file: ~/Type in the name of the Scheme program file. If it already exists, XEmacs will load it and display the beginning of it in the main window; otherwise, XEmacs will leave the main window empty and create a new file with the specified name, containing whatever you type into that window, when you ask for the file to be saved.
To save the file, click on the floppy-disk icon, third from the left end on the XEmacs toolbar. This will not automatically close the XEmacs window, since it is possible that you might want to re-edit the same file or to edit another file in the same session.
When you're ready to leave MathLAN and want to shut down XEmacs entirely,
click on the word File
at the left end of the menu bar at the
top of the XEmacs window and select Exit XEmacs
from the menu
that appears. It is not good practice to log out without first shutting
down XEmacs.
On MathLAN, it is conventional to use filenames ending in .scm
for Scheme programs. XEmacs understands this convention. When you ask to
edit a file with a name ending in .scm
, XEmacs goes into
``Scheme mode''; in other words, it turns itself into a tool that is
customized for the efficient development of Scheme code.
For example, a Scheme program is much more readable if definitions and expressions are carefully indented to manifest their structures. In Scheme mode, the Tab key automatically indents the line containing the editing cursor to the correct position. If you follow every carriage return with a tab, you'll always have correctly indented Scheme code.
Also, one of the hazards for novice Scheme programmers is keeping track of the parentheses and making sure that they are matched up correctly. XEmacs simplifies this process by automating it. Every time you type a right parenthesis in Scheme mode, XEmacs inserts it and then briefly highlights the corresponding left parenthesis (without changing the point at which subsequent characters will be inserted). By watching the highlighting as you type in right parentheses, you can ensure that each of them matches the appropriate left parenthesis, or make a correction if any of them does not.
To transfer definitions and expressions from an XEmacs editing window to SCM, you can simply cut and paste: Move the mouse pointer to the beginning of the definition or expression, press and hold the left mouse button, drag to the end of the definition or expression, release the left mouse button, move the mouse pointer into the window where SCM is running, and click on the middle mouse button. The selected text will be submitted to SCM, just as if you had typed it in from the keyboard.
Once a file is saved, however, there are simpler ways of getting SCM to read it. One is to name the file on the command line that starts up SCM:
bourbaki% scm -p1 foo.scm >SCM reads and processes the program in foo.scm (as if in batch mode) before going into interactive mode and issuing the prompt. This is handy if you have a library of definitions that you want to load in before beginning to interact with SCM. Any number of files may be named on the command line in this way.
It is also possible to load a Scheme program in the middle of a session,
using the (non-standard) load
procedure. Load
takes one argument, a string giving the name of the file containing the
program; it opens up that file, reads in and processes the contents,
returns #<unspecified>
when it is finished:
bourbaki% scm -p1 > (load "foo.scm") #<unspecified>A typical cycle in the development of a program is discovering an error by experimentation in SCM's interactive mode, fixing it in XEmacs by revising a definition in a Scheme program file, saving the file from XEmacs, reloading it with the
load
procedure in SCM, and resuming testing. It
is common to have both the hpterm window running SCM and the XEmacs window
containing an SCM program open, side by side, throughout a programming
session.