22C:54, section 2 - Programming Language Concepts

General Info.
Course Homepage
About 22C:54
Contacting Us

Homework & Grades
Grading Policies
Homework Directory

Q & A
Meeting outlines
Running Scheme
Scheme Library

Department Homepage
U. of Iowa Homepage

Valid HTML 4.0!
Valid CSS!

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

Revisions and web formatting for this page were done by Curtis Clifton.

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

This section is organized as follows:

  1. Starting SCM
  2. Working with Scheme
  3. Editing Scheme Files

Starting SCM

The Unix SCM interpreter is found in /usr/local/bin, so you must have this 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.)

To start SCM type the following at the shell's prompt:

% scm

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 will see something like the following:

SCM version 5d3, Copyright (C) 1990-1999 Free Software Foundation.
SCM comes with ABSOLUTELY NO WARRANTY; for details type `(terms)'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `(terms)' for details.
;loading /usr/local/scheme/scm/slib/require
;done loading /usr/local/scheme/scm/slib/require.scm
;loading /usr/local/scheme/scm/Transcen
;done loading /usr/local/scheme/scm/Transcen.scm

with the prompt  >  indicating that SCM is waiting for your input.

Now type your input, followed by a return. For example:

> (+ 2 3)

results in:


Return to Section Start

Working With Scheme

Exiting Scheme

To exit the scheme interpreter type:

> (exit)

or type a control-d.

Making a Transcript

To record your session with a Scheme interpreter 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.

Loading Files

It is very 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).

In SCM, you can also load files by simply listing the files you want to load on the command to start Scheme. For example, using scm to read in files named foo.scm and bar.scm, you would type at the operating system prompt:

% scm foo.scm bar.scm

We will use this trick to load some files by default whenever you start a Scheme interpreter. A standard way to do this is part of setting up your account for this course and is covered in Homework 1.

(Should you need to make such a script for yourself, you could proceed as follows. Make a shell script, say ~/bin/myScm, containing the following line.

exec scm file-I-always-load.scm "$@"

The line says to start Scheme and load the file file-I-always-load.scm and then any other files you specify on the command line. Be sure that there is a newline following this line. Remember to change the Unix file mode (permissions) on the shell script file you just created to make it executable. Suppose you called the shell script myScm (don't call it scm or scheme!), then you would execute the following:

% chmod +x ~/bin/myScm

To check that this has been set up properly, try the following from the Unix shell.

% rehash
% which myScm
% echo ~/bin/myScm

The responses to the last 2 commands should be the same. Note, this will not affect any scheme interpreters that you already have running.)

Scheme Advanced Topics

See the resources page for more about Scheme.

Return to Section Start

Editing Scheme Files

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 ~leavens/ui54/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 (BZZT!)

If you don't already know how to use vi, please don't learn it. The vi editor does not know how to format Scheme code (or for that matter C++). If you must use vi, invoke it 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 SCM for your Home Machine

The SCM Scheme interpreter used for this course is available on many platforms, including Unix, most flavors of Unix, Macintosh, and Windows. You can download the files necessary for compiling SCM from MIT here.

The default SCM package does not contain scripts for compiling on Macintosh; however, the Mac version of SCM is available here. The course staff can probably answer some questions about installation on various platforms. However, our experience on other than Unix and Macintosh is limited and the information on the links above is authoritative.

Getting Other Stuff for your Home Machine

In addition to the SCM interpreter you will also need your own copy of the course Scheme library. More information on the course Scheme library is available here.

To run shell scripts you may want to get Cygwin GNU software (bash, etc.) for your home (Windows) PC.

Even better, you can get GNU emacs for Windows either from that page or directly by ftp.

Return to top

Last modified Monday, January 1, 2001.

This web page is for the Fall 2000 offering of 22C:54 at the University of Iowa. The details of this course are subject to change as experience dictates. You will be informed of any changes. Thanks to Curt Clifton for help with these web pages. Please direct any comments or questions to Gary Leavens.