next up previous
Next: 2. Tutorial Exercises Up: No Title Previous: No Title

1. Getting started

The purpose of this section is to get you started using Scheme as quickly as possible. Start by looking at the subject web page labeled ``Using Scheme in 6.001''. You'll see there that the 6.001 Scheme system runs in the 6.001 Lab (34-501), on PCs running GNU/Linux, Windows NT4, or Windows 95 (sorry, no Macs or Athena); and that you can move among the different implementations as you require. If you have a PC capable of running Scheme, we suggest that you install it there, since it will be convenient for you to work at home a lot of the time. The 6.001 lab is probably the best place to work if you want help, since that is staffed by knowledgeable and friendly Lab Assistants.

Whether you're working with your machine or MIT's, the source of all useful information is the 6.001 web page: http://mit.edu/6.001

Starting scheme

The first thing to do is to get an implementation of Scheme and start using it:

Learning to use Edwin

In 6.001, we use a text-editing system called Edwin. Edwin is a implementation in Scheme of the Emacs text editor. If you've used Emacs before, you'll find Edwin virtually identical. If you have never used Emacs before (or even if you have) spend about 15 minutes going through the on-line tutorial, which you start by typing C-h followed by t. (C-h means ``control-h'': to type it, hold down the CTRL key and type h. Release the CTRL key before typing t.) You will probably get bored before you finish the tutorial. (It's too long, anyway.) But at least skim all the topics so you know what's there. You'll need to gain reasonable facility with the editor in order to complete the problem sets.gif To get out of the tutorial, type C-x k, which will kill the buffer.

Evaluating expressions

After you have learned something about Edwin, go to the Scheme buffer (i.e., the buffer named *scheme*).gif As with any buffer, you can type Scheme expressions, or any other text into this buffer. What distinguishes the Scheme buffer from other buffers is the fact that underlying this buffer is a Scheme evaluator.

Scheme programming consists of writing and evaluating expressions. The simplest expressions are things like numbers. More complex arithmetic expressions consist of the name of an arithmetic operator (things like +, -, *) followed by one or more other expressions, all of this enclosed in parentheses. So the Scheme expression for adding 3 and 4 is (+ 3 4) rather than 3 + 4.

Try typing a simple expression such as

1375
Typing this expression inserts it into the buffer. To ask Scheme to evaluate it, we type C-x C-e, which causes the evaluator to read the expression immediately preceding the cursor, evaluate it (according to the kinds of rules described in lecture) and print out the result. Try this.

Numbers are boring expressions to evaluate, since the printed representation of their value is the same as what you type. To see examples of evaluating more expressions, try these:

-37

(- 8 9)

(> 3.7 4.4)

(- (if (> 3 4)
       7
       10)
   (/ 16 10))

(* (- 25 10)
   (+ 6 3))
Observe that some of the examples printed above are indented and displayed over several lines for readability. An expression may be typed on a single line or on several lines; the Scheme interpreter ignores redundant spaces and line breaks. It is to your advantage to format your work so that you (and others) can read it easily. It is also helpful in detecting errors introduced by incorrectly placed parentheses. For example the two expressions
(* 5 (+ 2 (/ 4 2) (/ 8 3)))

(* 5 (+ 2 (/ 4 2)) (/ 8 3))

look deceptively similar but have different values. Properly indented, however, the difference is obvious.

(* 5 
   (+ 2 
      (/ 4 2) 
      (/ 8 3)))

(* 5 
   (+ 2 
      (/ 4 2)) 
   (/ 8 3))

Edwin provides several commands that ``pretty-print'' your code, indenting lines to reflect the inherent structure of the Scheme expressions (see Section B.2.1 of the Don't Panic manual). Make a habit of typing C-j at the end of a line, instead of RETURN, when you enter Scheme expressions, so that the automatic indentation takes place. Tab and M-q are other useful Edwin formatting commands.

While the Scheme interpreter ignores redundant spaces and carriage returns, it does not ignore redundant parentheses. Try evaluating

((+ 3 4))

Buffers and files

One could simply type all one's work into the *scheme* buffer, but it is usually better to store versions of your code in a separate buffer, which you can then save in a file.

The most convenient way to do this is to split the screen to show two buffers at once--the \*scheme* buffer and a buffer for the file you are constructing. You will need know how to split the screen ( C-x 2) and how to move from one half to the other (C-x o).

Starting in the *scheme* buffer split the screen by typing C-x 2. At this point both halves of the screen will show the same *scheme* buffer. Now choose a file name ending with .scm and type tt C-x C-ffile name. The half of the screen you are in will now show a buffer for the new file. You can type in this buffer and evaluate expressions in this buffer. The results will appear in the *scheme* buffer. If an error occurs during evaluation you must go to the *scheme* buffer to deal with the error.

To evaluate expressions, you can use any of several commands: you can use C-x C-e to evaluate the expression preceding the cursor, M-z to evaluate the current definition, or M-o to evaluate the entire buffer (this does not work in the *scheme* buffer. You may mark a region and use M-x eval-region to evaluate the marked region. Each of these commands will cause the Scheme evaluator to evaluate the appropriate set of expressions. Note that you can type and evaluate expressions in either buffer, but the values will always appear in the *scheme* buffer. See the Don't Panic manual for more details.

As a simple example create a new buffer called ps1-test.scm. In this buffer create a definition for a simple procedure by typing in the following (verbatim):

(define square (lambda (x) (*x x)))
Now evaluate this definition by using either M-z or C-x C-e. Next try evaluating:

square

(square 4)

In fact, if you typed in the definition exactly as indicated above, you should have hit an error at this point. The system will now be offering you a chance to enter the debugger. For now, go to the *scheme* buffer and type Q to quit the evaluation. (We'll discuss the debugger below in section 4). Go back to your ps1-test.scm buffer, and edit the definition to insert a space between * and x. Re-evaluate the definition, and try evaluating (square 4) again.

The system automagically maintains a special read-only buffer, called *transcript*, which keeps a history of your interactions with the Scheme evaluator. You can extract pieces of this buffer to document examples of your work for problem sets. To go to this buffer type C-x b *transcript*. You can go back to any buffer with the C-x b command.

Sending e-mail

That's it for this first part of the problem set, except for reporting the results. Compose an email message containing your definition of square, together with an excerpt from the *transcript* buffer showing that it works. If you are using the 6.001 lab, typing ctl-X m will open a mail buffer in which to compose your message. The system will ask for your own email address, so the recipient will know who sent the mail. You may insert work by copying from other buffers. To send your completed message type ctl-C ctl-C. If you're using your own machine, you can save your work in a file and then mail this using your favorite mailer.

Include in the message a brief, but warm and enthusiastic greeting to your tutor and to your recitation instructor. As part of the message, say which Scheme implementation you are using: 6001 Lab or your own machine (with which operating system?). If you don't know who your tutor and recitation instructor are, look on the 6.001 web page. The point here is not only to make sure you can send mail, but also to enable your tutor to add your email return address to the recitation section email list.

You can learn more about using Scheme in 6.001 from the Don't Panic manual. Also, at some point in the next couple of weeks, you should go back and run the Edwin tutorial again, to see what you glossed over the first time.

If you're a glutton for documentation, you can find an (almost) complete description of Scheme commands in the Scheme Reference Manual. The Scheme User's Manual has some details on MIT Scheme and Edwin. The Revised Report on the Algorithmic Language Scheme is the official (and not very readable) specification of the Scheme language. These documents are available on the web page, and they are included with the Scheme implementations. But you shouldn't need to look at any of them, because the material will introduce new elements of Scheme as you need them.


next up previous
Next: 2. Tutorial Exercises Up: No Title Previous: No Title

Hal Abelson
Sat Jan 31 15:55:15 EST 1998