Running Scheme

This page is organized as follows:

  1. Running Scheme on Departmental Unix Machines
  2. Running Scheme on Your Home Machine

Running Scheme on Departmental Unix Machines

The departmental Unix machines are the recommended platforms for course work.

This section is organized as follows:

  1. Starting DrScheme
  2. Working with DrScheme
  3. Other Interpreters
  4. Editing Scheme Files

We recommend using the DrScheme development environment in 342. This web page gives information about other Scheme systems, including Chez Scheme, which you can also use if you wish, but DrScheme should be enough for the course.

Starting DrScheme

The DrScheme system can be used either on the department Linux machines, or you can download it to your home machine. There are versions for both Macs and PCs running Windows, as well as Solaris. If you download it for your home machine, see the Getting Other Stuff for your Home Machine section below for details.

On the department Linux machines, the interpreter is found in /opt/plt/bin, so you must have the appropriate directory in your shell PATH for the interpreters to work. (But this is the default on the department servers, so you shouldn't have to do anything special to set this up.)

On Unix, you can start the DrScheme system by using the following command at the Unix Shell's prompt:

$ drscheme

Note: In code examples, green text is program output, yellow bold text is user input, and cyan text is sample code.

and then hit the "return" (or enter) key. Be sure your input is in lower case (not CAPITALIZED). You should see a window pop up to load the system.

If you see the error message "DISPLAY environment variable not set and no -display argument", this occurs because you are using ssh to access a department machine and you have not forwarded the X windows connection to the machine you are connecting from. The simplest fix for this is to run DrScheme directly on the machine you are on, instead of remotely on another machine via ssh. If you have a home computer, you can install the DrScheme system on that machine. If you don't have your own machine, come in to the department where DrScheme is installed on the machines and use it locally. Or you can forward the X windows connection, for example by using ssh -X to connect.

Working with DrScheme

Selecting the Programming Language Dialect

Once you have started DrScheme, a very important dialog comes up about selecting a (programming) language to work with. Most of these are simply dialects of Scheme.

We suggest selecting the "Typedscm" language. (However, note that this only works if the course library has been installed; for a home installation, see the library page.) To select the "Typedscm" language, open the "Language" menu, and Then select "Choose Language..." and from the resulting popup, select the language named "Typedscm, typed extension to EoPL(2e)". Then press the "OK" button.

Now, push the "Run" button to restart the interpreter. You should see that the language selected is now Typedscm.

If Typescm is not an available language, you can start with the "Beginning Student" language. To fix this problem, which you will need to do eventually, you need to be sure to run Setup PLT.exe from the PLT directory (as described in the Setting Up DrScheme section of the course scheme library web page).

Normally you shouldn't have to change any of the defaults shown by the "Show Defaults" button. The only one that seems to matter much for testing homework and such is the one under "Input Syntax", which should have the "Case Sensitive" parameter checked. This is the default, however, so you shouldn't have to mess with it.

Selecting the Course's DrScheme Teachpack

The course's teachpack puts some course-specific procedures into the DrScheme environment. We use this primarily for testing homework.

Selecting the teachpack should be done after selecting the Typedscm language (see above).

To select the teachpack, you again use DrScheme's language menu. From the Language menu, select "Add teachpack...". From the file selection dialog that appears, select the file drscheme-342-teachpack.scm.

(If for some reason you don't see that file, you can navigate to ../collects/lib342, which is the same as the PLT directory's subdirectory collects/lib342, and select the file with the same name.)

Now, push the "Run" button to restart the interpreter. You should see that the teachpack is loaded. One way to check this is to type test-hw1 at the prompt, it should show you an output, like #<procedure:...est-homework.scm:9:6>.

Interacting with DrScheme

You can type expressions into the bottom frame of the window that appears next, at the >  prompt. The top window is where you can type definitions. These can be run and saved in files. See the help menu, especially the "Help Desk" menu item, for more information. It's worth taking the "Tour" that appears under the heading "Software" to see what's in the system.

Making a Transcript in DrScheme

There are three ways to print a record of your session with the DrScheme interpreter. First you can select "Print Interactions..." from the File menu. Or you can select "Save Other" from the File menu, and then choose "Save Interactions as Text"; after doing this, print the text file as you would normally. Or, if you wish, you can copy and paste the text you want to print into a text file, and print that in whatever way you normally do that. Note that DrScheme does not support Scheme's "transcript-on" facility.

Loading Files

In DrScheme, the easiest way to load a file is simply to open it, using the File menu, and then to press the "Run" button. Pressing the "Run" button loads the current text in the definitions part of the same DrScheme window (or tab). To load text in other windows (or tabs), be sure these files are saved first, then use load as described below.

It is often convenient to put your code for Scheme programs in files, and have Scheme read those files. A standard way to have Scheme read a file is to use the load procedure. The advantage of this is that load can be called from a program; for example, you can have one file that loads several others. Another advantage is that you can use load after you have already started Scheme. An example of using load after starting Scheme is as follows:

> (load "foo.scm")

If your files have errors you will see an error message; for example,

> (load "foo.scm")
;loading foo.scm

"foo.scm", line 2: ERROR: list: end of file in (2)

is an error message that means that foo.scm is missing one or more right parentheses (a common mistake).

If the file is not found, it is possible that Scheme thinks it is in a different directory than you think it is in. To check the current directory, use (current-directory). To change the current directory to "c:\cs342\homework", use (current-directory "c:\\cs342\\homework").

If the file contains a module, then instead of using load, you should use require, as in the following example.

> (require (lib "lambda-1-exp.scm" "lib342"))

Return to Section Start

Other Interpreters

Although it's not needed for this course, it's also possible to work with the Chez Scheme interpreter for the course. This interpreter is also available free for a variety of machines.


To exit Chez Scheme interpreter type:

> (exit)

or type your end-of-file character (control-d on Unix).

Making a Transcript

To record your session with a Chez Scheme in a file named trans.out, that is to put your inputs and its outputs in the file trans.out, type the following to Scheme:

> (transcript-on "trans.out")

This must be typed exactly as above, with the double quotes ("). To make a transcript in some other file, write that name instead of trans.out.

Scheme Advanced Topics

See the resources page for more about Scheme.

Return to Section Start

Editing Scheme Files

The easiest way to edit Scheme files is to use DrScheme itself. Simply open the file and edit in the window that appears.

You can also edit Scheme files externally to DrScheme, and load the file when needed. The following describes working with Emacs and other editors to do this, which is entirely optional.

Editing Scheme Files With Emacs

Of the standard Unix editors, emacs has the best support for Scheme programming (and for other programming languages also). In addition to matching parentheses (when you type  )  it shows which  (  matches it) for you, emacs knows how to indent Scheme programs to make them look good. But before emacs will do this for you, it must know that you are editing a Scheme program. The best way to tell emacs that you are editing a Scheme program is to use the customizations found in the file $PUB/docs/sample-.emacs.

Either copy that into your file "~/.emacs", or edit the relevant lines into your file "~/.emacs".

With the relevant customization of your file ~/.emacs, when you are editing a file whose name ends in ".scm" (or ".ss" or ".def") then that is a Scheme file.

Be sure that all your Scheme file names end in ".scm" ! For example "member.scm" is a good name, but not "homework".

To find out about scheme-mode in emacs, edit a scheme file and then type C-h m (that is, control-h and then "m"). You will see a table of commands that looks like the following (which has been simplified; what you see in emacs by typing C-h m is the truth).

 key             binding                           
 ---             -------                           
 RET             newline-and-indent                
 C-j             newline                           
 C-c             Prefix Command                    
 C-x             Prefix Command                    
 ESC             Prefix Command                    
 DEL             backward-delete-char-untabify     
 C-c C-k         scheme-compile-file               
 C-c C-l         scheme-load-file                  
 C-c C-z         switch-to-scheme                  
 C-c C-c         scheme-compile-definition-and-go  
 C-c C-r         scheme-send-region                
 C-c ESC         Prefix Command                    
 C-c C-e         scheme-send-definition            
 C-x C-e         scheme-send-last-sexp             
 ESC C-x         scheme-send-definition            
 ESC C-q         indent-sexp                       
 C-c ESC c       scheme-compile-definition         
 C-c ESC r       scheme-send-region-and-go         
 C-c ESC e       scheme-send-definition-and-go     

For example, if you type C-x C-e (control-x and then control-e), it sends the previous s-expression (a parentheses balanced thing you typed) to the Scheme process.

Interacting With Scheme From Emacs

To run Scheme from within Emacs, start emacs, and then use the command M-x run-scheme. (That is, press the ESC key, then  x  then at the prompt  run-scheme ). This will create a buffer called *scheme*, which will be in "inferior scheme mode". You will soon see the scheme system starting, just as if you had run it directly from the shell. (The name of the command run is given by the Emacs variable "scheme-program-name".) You can type expressions at the prompt, and then by pressing "return", you will see them evaluated. To find out about inferior scheme mode in emacs, from the *scheme* buffer type C-h m. You will see a table of commands that includes those in the table above, and some additional ones.

 key             binding                           
 ---             -------                           
 DEL             backward-delete-char-untabify     
 C-x             Prefix Command                    
 C-d             comint-delchar-or-maybe-eof       
 RET             comint-send-input                 
 C-c             Prefix Command                    
 C-down          comint-next-input                 
 C-up            comint-previous-input             
 ESC             Prefix Command                    
 C-x C-e         scheme-send-last-sexp             
 C-c C-k         scheme-compile-file               
 C-c C-d         comint-send-eof                   
 C-c C-p         comint-previous-prompt            
 C-c C-n         comint-next-prompt                
 C-c C-l         scheme-load-file                  
 C-c C-e         comint-show-maximum-output        
 C-c C-r         comint-show-output                
 C-c C-o         comint-kill-output                
 C-c RET         comint-copy-old-input             
 C-c C-\         comint-quit-subjob                
 C-c C-z         comint-stop-subjob                
 C-c C-c         comint-interrupt-subjob           
 C-c C-w         backward-kill-word                
 C-c C-u         comint-kill-input                 
 C-c C-a         comint-bol-or-process-mark        
 C-c C-x         comint-get-next-from-history      
 C-c SPC         comint-accumulate                 
 C-c ESC         Prefix Command                    
 ESC C-q         indent-sexp                       
 ESC C-x         scheme-send-definition            
 ESC C-l         comint-show-output                
 ESC s           comint-next-matching-input        
 ESC r           comint-previous-matching-input    
 ESC n           comint-next-input                 
 ESC p           comint-previous-input             

The basic addition is that when you type a return (RET), what you typed is sent to the Scheme interpreter. You can edit the input using emacs commands, but note that whatever is on a line at the time you type return (RET) is sent to Scheme, and is not affected by further editing. However you can go back up in the buffer and send an edited expression again by placing your cursor just to the right of the last right parentheses, and typing return. Another handy feature is to use M-p (ESC then "p") which recalls a previous input you typed; you can do this several times to get earlier inputs. These earlier inputs can also be edited.

You do not need to separately exit Scheme and emacs, as exiting emacs will kill Scheme. (However, if you are using emacs to run Scheme at home from within Microsoft windows, it is best to exit Scheme before exiting emacs.)

The Scheme running under emacs does not automatically know about changes you make to Scheme code files or other emacs Scheme buffers. You have to either explicitly load the file (use load-file in Scheme as described above), or use a scheme-send command (such as C-x C-e) in the buffer where you are editing your code. (Note that these have to be in the same emacs process)

Editing Scheme With VI

The vim implementation of the vi editor does a good job of indenting Scheme source code if you have the ``filetype detection'' mechanism turned on. In this case, vim will load the scheme syntax and indentation files, and automatically indent Scheme code exactly as DrScheme does. It also has good parentheses matching (when the ``showmatch'' option is turned on).

For older vi implementations it may help to use vi with the "-l" option, which will make it match parentheses for you.

You can also use your vi keystrokes in emacs. To do this, simply execute

M-x viper-mode RET

when in emacs. See also the emacs help by typing

M-x help-for-help i

and then select the topic "Viper Mode" for more information. This allows you to use vi's editing commands, but all the features of emacs. It also allows you to use the mouse in this emulation, something you can't really do in vi.

If you decide you want to use viper-mode in emacs all the time, put the following line in your .emacs file.


Return to Section Start

Return to top

Running Scheme on Your Home Machine

Getting DrScheme for your Home Machine

Download the DrScheme system, which is used for this course. It is available on many platforms, including Windows, Macintosh, Solaris, and several flavors of Unix.

Getting Other Stuff for your Home Machine

You need to get, and keep updated, your own copy of the course Scheme library and the homework files. See the course Scheme library page for details on how to do this.

Other Optional Stuff

To run shell scripts you may want to get Cygwin GNU software (bash, etc. for your home (Windows) machine. This is especially handy if you use SCM or Chez Scheme.

If you like editing with emacs, you can get GNU Emacs for Windows NT and Windows 95/98 for you home machine.

Return to top

Last modified Monday, February 13, 2006.

This web page is for the Spring 2006 offering of Com S 342 at Iowa State University. The details of this course are subject to change as experience dictates. You will be informed of any changes. Thanks to Curtis Clifton for help with these web pages. Please direct any comments or questions to Gary T. Leavens.