Lecture 2: An Introduction to the Scheme Language

The UMASS Scheme System

How to start up UMASS Scheme.
Editing Files and Running Scheme Code.
Changing your default font for VED under X.

The Scheme Language

Typographic conventions used in these notes
Simple values in Scheme.
Variables in Scheme.
(define variable expr) creates a new variable and gives it a value
Expressions in Scheme.
So what are functions in Scheme?.
An expression (lambda(v1..vn) expr) denotes a function .
The Debugger.
The function load loads a file of Scheme code.

How to start up UMASS Scheme.

To start up UMASS scheme type scheme as a Unix command. You will get a message like this:

  Sussex Poplog (Version 14.5 Tue Feb 22 15:01:28 WET 1994)
  UMASS Scheme (Version 1.0)
  initialising X windows

  Sussex XVed (Version 2.0 (motif), SCHEME Tue Sep 12 12:14:16 EDT 1995)
  X Server: MIT X Consortium
  initialising X windows

[Note for experts: getting UMASS Scheme depends on an alias in the class .cshrc file, which itself depends on your own ~/.cshrc file. If you change this file, you may find yourself running a version MIT Scheme which does NOT accord with the IEEE standard required for this class.

UMASS Scheme is implemented under the Sussex Poplog system developed at Sussex University (Falmer UK), and employs the X-window system developed at MIT (Cambridge MA). ]

If you are logged in in the EDLAB, you will get a version which is customised for running under X-windows. In this version, you will be able to edit and test programs entirely within UMASS Scheme system if you wish.

If you are logged in remotely, for example from home, and do not have an X-server on your local machine, you will get a version of UMASS Scheme which is customised for running under a terminal emulator. If your emulator matches a VT100 terminal, this should be OK, but you may need to set the term environment variable before you run your Scheme. If Scheme is unhappy with your terminal it will say so.

If you are logged in remotely from a machine which is running an X-server, for example another workstation in UMASS, or your own machine running an X-server using the ppp protocol, you should do:

where mymachine is the net-address of the machine you are logged in on. This will allow UMASS Scheme to open windows on your machine.

Editing Files and Running Scheme Code.

Any editor can be used for writing Scheme. However you are best advised to use either the built in VED editor of UMASS Scheme, or the EMACS editor, both of which provide help in laying out your code.

Under X-windows, a small "control panel" will pop-up when you start Scheme. This has a box labelled "File:". This contains the name of a default file ("unnamed.scm") that you can edit using the built-in VED editor. If you want to change this, put the mouse in the box, drag the mouse along the length of the name - it will now reverse its colours. Then type in the name of the file you want to edit, and hit the Do Edit button. After a brief pause a new window will appear with your file (possibly empty) in it.

If it is a file to contain text in the Scheme language, the name should end with ".scm".

There is also "Exit" button to kill the Scheme system.

Each edit window contains a "menu-bar" across the top. This is used in a style akin to that of the Macintosh. Currently this has the following menus attached:

Additional help with editing is found under the "287" menu. This online material is found in the directory $cs287/public_html.

Changing your default font for VED under X.

If you want to use a different font, add a line of the form XVed*font: to your .Xdefaults file. You can obtain a (long) listing of available fonts by doing xlsfonts as a Unix command.
XVed*font: 7x12  # The default size - rather small for some.
XVed*font: 9x15  # A bigger size, which you may like better.

You can also change the font of a particular window by doing

Where font is the name of a font.

The Scheme Language

Typographic conventions used in these Lectures

All expressions and other constructs of the Scheme language which occur in these lectures are set in a typewriter font, An example of typewriter font is (define pi 3.14159) .

Typewriter fonts are characterised by being fixed-width (every letter occupies the same amount of space on the page). In the HTML browser which is being constructed to let you view these lectures from within UMASS Scheme, Scheme expressions will appear in red, since the VED editor always uses a single fixed-width font and provides no means of distinguishing text by using a different font, only by making variants on a given font.

Where it is important to make an expression of the language stand out, it will be placed indented as a line or lines of text separated from the English commentary. For example:

Grammatical constructs are specified using a bold version of the typewriter font. For example, we specify one form of the Scheme define statement by

Here, because variable is in a bold font it means that you can write any Scheme variable in place of it; likewise because expression is in a bold font it means that you can write any Scheme expression in place of it. So, the previous statement:

is an instance of the grammatical form

Simple values in Scheme

Scheme operates on the usual simple data-objects, that is to say data-objects that can best be thought of as having no internal structure.

23   is an integer
3.4  is a real
3/4  is a rational (WARNING - UMASS Scheme does not currently input rationals
                    but it will generate them and print them out).

Variables in Scheme

A scheme variable is a sequence of letters (a..z A..Z) or digits (0..9) or signs (+ - * / ...) which are terminated by "white-space" or by certain special characters, of which parentheses are the most important. In fact, most combinations of non-whitespace characters which Scheme does not recognise as being a number are treated as a variable. Usually, people will choose helpful names for variables.

For example the following are variables:

        x y x_23 34x the-fat-cat + +1

Warning - Scheme gives some variables initial values, for example +, *, / . It is not a good idea to use these variables for your own purposes!

define creates a new variable, gives it a value

Variables usually have values. We say that a variable is bound to a value when it is associated with the value. Scheme has a statement which creates a new variable and binds it to the value of an expression

For example:

binds the variable pi to have the value 3.14159

Expressions in Scheme

Expressions have a very uniform syntax in Scheme. An expression can be a constant or a variable, or it can be a sequence of expressions enclosed in parentheses (sometimes called a combination).

In the case of a combination, the first expression is regarded as a function while the others are its arguments. Thus:

     (+ 3 4)

is an expression, whose function is '+' and whose arguments are 3 and 4.

When Scheme evaluates an expression of this form, it evaluates the function and arguments, and then applies the function to the arguments. Evaluating the above expression we get


means "the result of subtracting 4 from 5", or in ordinary mathematical notation 5-4.

A more complicated example is:

Here we evaluate (* 4 5) to get 20, and then add 3 to get 23.

So what are functions in Scheme?

Well, actually functions are pieces of machine code (in effect subroutines for those who have taken CMPSCI 201). So the value of the variable '+' is a piece of code for doing addition.

[Note you can actually redefine Scheme standard functions. This is NOT recommended for novices, for you may lose important capabilities]

Making your very own function

A Scheme function is specified by the syntax

here formals is (usually) a sequence of variables enclosed in parentheses, and body is a sequence of expressions. For the functional use of Scheme, only one expression is needed in the body.

Note that this is not an application of the function lambda, but is what is called a "special form" in Scheme.

For example

is the function which squares its argument. So

evaluates to 9.

The rule for evaluation of a lambda-function applied to arguments is to (a) evaluate all the arguments, and (b) strip off the 'lambda' and substitute the values of each argument for the corresponding formals, (c) evaluate the body of the lambda with the values substituted.

   ( (lambda (x) (* x x)) (+ 3 4))

Evaluate the argument

Strip and substitute for corresponding formals (x=7) :

Evaluate body

This may seem a funny way of defining functions - in Pascal we would give the corresponding function a name, like "square". In Scheme, this is easily done

Scheme responds:

and now we can use it

Scheme responds:

An example of a lambda expression with more than one argument is

And an example of its use is:

Evaluate the arguments

Strip and substitute, x=7, y=5:

and evaluate

The Debugger

This is accessed by hitting the "Debugger" button on the Scheme Control Panel. It is operated by a panel which contains the following buttons:

Generate Debug Code:
This is normally true, so that Scheme generates code for the debugger by default. Turn it off to create smaller, faster programs.
Stop at All Breakpoints:
This is normally false. When set true, Scheme will stop after it evaluates every compound expression such as (+ x 2). It will print out "frames" for the top n functions you are currently executing, where n is set by the "Number of Frames" slider.
Number of Frames:
This slider determines how many function-frames you will see.
Go To Next Breakpoint:
Execution continues until the next (automatically generated) breakpoint. Breakpoints occur after an expression has been evaluated.
Skip Current Function:
The execution of the current function will be completed without stopping at any breakpoints.
Prints out help information about the debugger.

The function load loads a file of Scheme code.

The function application (load "file") will load any file as Scheme program.