Getting started with Scheme

Here are a few handy links:

whuang@cs.rpi.edu; email
Last update: August 20, 2002


Getting and running MIT Scheme

The first thing is to download the lastest version of MIT Scheme. You can also find download instructions there which are pretty straightforward.

Note that the documentation (in HTML form) comes with the distribution. Find it, and create a bookmark in your web browser.

There are three basic ways you can run MIT Scheme (in order of my recommendation):

  1. Run MIT Scheme as an inferior process under gnu-emacs or xemacs. (This option is not available under Windows.)

  2. Run Edwin, the emacs-like editor that comes with MIT Scheme.

  3. Run MIT Scheme standalone (and edit your code in some other editor).

I do not recommend running MIT Scheme standalone because you do not have command line editing. Besides, I strongly recommend using emacs or Edwin to write your code, so you might as well run your code via one of these methods.

If you're using Windows, you don't have the choice of using Scheme through gnu-emacs. However, you may want to download gnu-emacs anyway and use it to edit your code (because it is more user friendly than Edwin) and then using Edwin to run and debug. You can download emacs for Windows from one of the mirrors listed in the Emacs Windows FAQ.

If you're using UNIX of some sort, I suggest using gnu-emacs. Here's a comparison of the advantages and disadvantages of Edwin and gnu-emacs:

Edwingnu-emacs
+ scheme editing mode with electric parentheses + scheme editing mode electric parentheses
+ M-p and M-n scroll through command history - Can only yank previous command with C-c C-y
- Doesn't work with the X-cut buffer + X-cut buffer can be used to cut and paste between windows
- Missing some emacs features + It's real emacs!
+ Has nice debugging interface - Text debugging interface
- No pull down menus + Pull down menus

As a gnu-emacs "power user", I find Edwin kind of annoying because of missing gnu-emacs features, so I work using the gnu-emacs interface and only use edwin for extended debugging sessions. You may very well want the (relative) user-friendliness of emacs over Edwin, but if you know (or are willing to learn) emacs well enough that you don't need the pull down menus and such, then you may prefer Edwin.

Running Scheme under gnu-emacs

If you want to run Scheme as an inferior process in gnu-emacs or xemacs (this is not an option on Windows machines), then you'll need to:
  1. Download the xscheme.elc file. This is a byte-compiled elisp file that tells emacs how to run and interact with MIT Scheme.

    This file should replace the xscheme.elc file that comes with emacs. You'll have to find the appropriate directory on your system. On my Mandrake Linux 8.2 system, this is the directory: /usr/share/emacs/21.1/lisp.

  2. Add the following line to your ~/.emacs file

    (load-library "xscheme")

  3. Start (or restart) emacs, and it will load MIT Scheme's xscheme library.

To exit emacs, type C-x C-c (that's control-x followed by control-c).

Running Edwin

In Windows, you can start Edwin by simply using the shortcut from the Start menu, but in UNIX, you need to give the command:
scheme -edwin -edit
You will find it convenient to create an alias. For example, I have added the line:
alias edwin='scheme -edwin -edit'
to my ~/.bashrc file.

To exit Edwin, type C-x C-c (that's control-x followed by control-c).

Running Scheme standalone

You can run scheme all by itself, write your code in some editor (preferrably emacs), and then load your code via the command:
(load "filename.scm")
But again, I don't recommend this option.

To exit scheme, type "(exit)".


Using Emacs/Edwin with Scheme

Gnu-emacs and Edwin offer many special capabilities for working with Scheme code. However, there is a little bit of a learning curve for using emacs. This section suggests some resources for learning emacs and then describes the two types of buffers you will use in Edwin and emacs: a Scheme interaction buffer and a program file buffer.

Learn emacs

If you are new to emacs (or rusty), then you should go through the built-in tutorial. After starting emacs or Edwin, type C-h t (that's control-h followed by 't'). This tutorial will take you through the basics of editing, buffers, and saving/loading files.

Here are some other emacs resources you may find useful:

The Scheme interaction buffer

When you start Edwin, you automatically get a scheme interaction buffer called *scheme*. In gnu-emacs, giving the command M-x run-scheme will start Scheme in a scheme interaction buffer (also called *scheme*).

In the Scheme interaction buffer, you can type Scheme expressions and use one of the following commands to send them to the Scheme process to be evaluated:

Experiment with these commands until you understand how to send the expression you want to the Scheme interpreter.

If you make an error, you will get a beep and some error messages about calling RESTART. From here, you can get back to the top level of the Scheme interpreter by typing C-c C-c. You can also enter the (regular) debugger by typing (debug). In Edwin, it may offer to start the debugger for you.

In Edwin, M-p and M-n will take you through the previous commands sent to the scheme interpreter. In emacs, C-c C-y will yank the previous command.

Scheme program buffers

When you load a file with the extension .scm, Emacs/Edwin will enter Scheme mode. There are a number of commands to help you edit and run scheme code.

The M-z and C-x C-e commands work the same in this buffer as they do in a scheme interaction buffer. The result will be printed in the *scheme* buffer but also briefly displayed under the mode line of the emacs/Edwin window. These commands are useful for sending a single function to Scheme while you are developing a program.

The M-o command sends the entire buffer to the scheme interpreter.

Emacs/Edwin will indent you code for you! just press tab at the beginning of each line, and the cursor will move to the proper point to begin typing.

When typing comments, M-j will act like the return key except that it will start the next line with the comment character. In emacs,M-q will reflow a paragraph and properly comment it. (Edwin can do this too, but see below for how to set this nondefault keybinding in Edwin.)

For more information

There are a number of other emacs/Edwin commands that you will find useful. Refer to:

A few notes on using Edwin


Top 10 keybindings in Edwin/Emacs for MIT Scheme

These are keybindings specific to using MIT Scheme. I STRONGLY recommend that you spend some time learning how to use the emacs editor itself, i.e. learn the basic keybindings for moving the cursor around, switching buffers, killing and yanking text, etc. The time you spend will be more than repaid in your productivity.
  1. TAB properly indents your scheme code

    Type a line of code. Hit return. Hit TAB. Type the next line of code. Repeat.

    OR, put the cursor an any point on a line of improperly indented code. Hit TAB, and the line will be indented the proper amount (relative to the lines above it, so start at the top if you need to do multiple lines, or use C-M-q).

  2. C-x C-e sends the expression to the left of the cursor to the Scheme interpreter. This works both in the Scheme interaction buffer as well as in scheme-mode buffers where you are editing your code.

    This command looks left from the cursor until it has a complete expression and then sends that to the Scheme interpreter.

  3. M-z sends the current expression to the Scheme interpreter. This works both in the Scheme interaction buffer as well as in scheme-mode buffers where you are editing your code.

    This command looks for an open parenthesis in the leftmost column and sends all the text from there up to the cursor position, and then to the right of the cursor until all parentheses are balanced.

  4. C-c C-s when done in a scheme-mode buffer, will switch to the Scheme interaction buffer.

  5. In Edwin, M-p and M-n in the Scheme interaction buffer will scroll through the history of expressions that you have sent to the Scheme interpreter. (In gnu-emacs, you can use C-c C-y to yank the previous command.)

  6. C-c C-c sends an interrupt to the Scheme interpreter.

  7. M-o from a scheme-mode buffer, will send the entire buffer to the Scheme interpreter.

  8. C-M-f and C-M-b move the cursor over the previous and next (respectively) parenthesis balanced list. (C-M-p and C-M-n perform the same functions.)

  9. C-M-a and C-M-e move the cursor to the beginning and end of a definition, respectively.

    Your code should be indented properly for these to have the desired effect.

  10. C-M-q properly indents an entire definition.

    The cursor should be on the opening parenthesis of the define for this to work properly.