The Scheme we will be using is a Windows program called MIT Scheme, developed by a group at the Massachusetts Institute of Technology (a DOS program called PCScheme is also available).
How to start MIT Scheme :
[Start][Programs][MIT Scheme][MIT Scheme (edwin + compiler)]
The result is a window with the MIT Scheme interpreter. (Note:
you may automatically get a second window with the Edwin editor.
That will work almost the same. Read on for instructions.)
Scheme saved on Wednesday August 6, 1997 at 2:39:38 PM Release 7.4.2 Microcode 11.151 Runtime 14.168 Win32 1.0 Edwin 3.90 SF 4.31 Liar (Intel i386) 4.106 1 ]=>The last line that appears is the interpreter's prompt. The 1 at the beginning of the line indicates that you are at the top level (you are not in the error handling facility).
How to exit Scheme:
1 ]=> (exit)
How to type in commands. Scheme uses "prefix" notation to call
functions. In other words, the function name comes first, then the
arguments. To call a function, it is enclosed in parentheses (a list)
along with its arguments. For example:
(+ 1 2)
;Value: 3
In the above example, the function named + was called with the
arguments 1 and 2. The + function returns the sum of its arguments,
in this case 3. In Scheme, the function name goes first, and
then the arguments to the function. This is called "prefix" notation
as compared to infix notation found in algebra (2+3, for example).
One big advantage of prefix notation is that you can use one function
with a varying number of arguments, without adding copies of the function.
For example, it is perfectly acceptable (and useful) to do the following
in Scheme:
(* 21 33 94 88)
;Value: 5732496
In other languages with infix notation, one must repeat the multiply
function between each pair of numbers:
21 * 33 * 94 * 88
New variables are created using the function define as follows:
(define name "Kris")
;Value: name
This command created a new variable (mailbox) named name and put the value "Kris" (the string Kris) in the mailbox. The return value of this function is not terribly important (although it tells us that the variable was successfully defined). The important work of the define function is that it has the side-effect of creating the new variable in the environment. That variable will then be available for the rest of the Scheme session in the interpreter. Its value will remain the same until some command changes it.
New functions or procedures are also created using the function
define. New functions can be created with the keyword lambda,
or with a shortcut definition. The two function definitions below are
equivalent (they each create a function with the exact same behavior).
(define hello
   (lambda ()
     (display "Hello world!")))
(define (hello2)
   (display "Hello world!"))
Each of these definitions creates a function with no arguments that will print the string "Hello world!" to the screen. The first procedure has the name hello, while the second command has the name hello2. The define command creates a new symbol (mailbox) with the function name and put the function body as the 'value' in the mailbox.
In Scheme, variable and procedure/function definitions at the top level (global) are always available. The programmer builds up a collection of functions with actions (recipies), and variables (mailboxes) with values. You can invoke (call, execute) a function at any time. It is easy to build small, useful functions, test them separately, and have them work together to solve complex tasks.
Once you have started the Edwin editor, you will have a second window. You will not be able to enter commands into the first window, however Edwin starts with a buffer called *scheme* that will interact with the interpreter and print out the results you get. This buffer is also identified with the string REPL in the status bar. This is short for Read-Evaluate-Print Loop (the actions the interpreter takes each time you issue a command).
Edwin works slightly differently than the prompt window, because it is also an editor. It does not evaluate commands when you press [Return]. Instead, use [Ctrl-x][Crtl-e] to evaluate the expression right before the cursor position in the file. Your lab manual shows other commands available to evaluate other expressions in the editor window.
The Edwin editor is started from Scheme by executing the function (edwin).
In Edwin, functions are performed with keystrokes, similar to a DOS-based wordprocessor, like WordPerfect. An exception is that Edwin does not use the function keys, rather it uses the Control and Meta (escape on the PC keyboard or alternatively Alt) keys along with letters, numbers and symbols from the main section of the keyboard. The arrow keys will also let you move around in the file you are editing. With a little practice you will find that you can type and edit much faster with an editor like Edwin than with an editor like Notepad.
The Edwin editor has (a subset of) the same key bindings as the Emacs editor. Emacs is one of the most powerful and popular editors available, and is installed on the IT suns where your email is read. Emacs is available at no charge for most computer platforms, so your time learning the commands will not be wasted.
Note that the Control key is held down while the second key is pressed. The Meta key is pressed once and released before pressing the next key. (Note: You may use the Alt key instead of Esc for Meta. To do this, you must hold down the Alt key while pressing the second key once.)
In Edwin, the following keystrokes will perform common editing functions:
The procedure define goes first and then the name of the new variable to be created, and third, the value you would like the new variable to contain. The value is optional, but it is usually a good idea to put something in each variable when you create them. This is called initializing the variable.
Examples:
(define number 10.5)
(define name "Kris")
These statements created two new variables, number and name. Number is given the initial value of (a number) 10.5. Name is given the initial value "Kris" (a string of text).
Variables are used by placing their name in a statement where we
want the value to be used. For example, if we have a variable such as
name with the value "Kris", then typing the
variable's name to the Scheme interpreter will return its value, as
follows:
name
will return
"Kris"
Issuing the command:
(display name)
will print
Kris
and return nothing.
Note: The display command does not print the double quotes that are present in strings, so the second example does not have the quotes.
If we have a variable number with the value 10.5,
then typing in the variable's name to the Scheme interpreter will
return its value, as follows:
number
10.5
Issuing the command
(* number number number)
will return
1157.625
We can change the value of a variable with an assignment statement (or by re-defining it). The assignment statement you will probably use the most is set!. The syntax of the set! statement is shown next:
(set! < name > < new-value >)
The function name is enclosed in a list along with the variable's
name and the new value to be put in the variable's "mailbox". Assuming
we have the two variables shown above, the following two statements will
change their values.
(set! name "Pat")
(set! number 42)
The variable name will now have the value the string "Pat" and the variable number will now have the value 42.
The function define goes first, then a list containing the name of the new procedure being created, followed by the formal names of the arguments to the function. There will be one name for each argument the new procedure will receive when executed. The rest of the function definition, called the body, contains the computations and actions to be performed by the procedure. There can be any number of actions in the body of the function.
Example:
(define (reverse-name middlename) ; function from lab 6
(let
((firstname "Blank")
(lastname "Blank")
)
(newline)
(display "Enter your first name surrounded by double quotes: ")
(set! firstname (read))
(newline)
(display "Enter your last name surrounded by double quotes: ")
(set! lastname (read))
(newline)
(display "Your name switched around is: ")
(display lastname)
(display " ")
(display middlename)
(display " ")
(display firstname)
(newline)))
Answer: the same as "built-in" Scheme functions, enclose the function name and its arguments in parentheses (a list).
(< function-name > < arguments > *)
Below is the syntax of the Scheme functions you will be using in lab 6. All the items shown between angle brackets (< >) will be replaced by the desired items when the function is used.
   ;Definition: create a new variable (mailbox) and give it an initial value
(define < variable > < value for variable > )
   ;Definition: create a new function/procedure and save with a name (mailbox)
(define ( < procedure name > < procedure arguments.. > +)
   ( < function calls > ...) )
   ;Assignment: modify the value of an existing variable
(set! < variable > < new value > )
   ;I/O, Output: display something on the screen
(display < item > )
   ;I/O, Input: get data from the keyboard (user)
(read)
   ;Definition: create local variables (within a function/procedure)
(let (( < variable > < value >) +)
     < statements > )
Example function definition (from lab 6):
(define (thrice number) (* number 3))The function thrice, shown above, takes one argument (which should be a number) and returns the number obtained when the argument is multiplied by 3.
Another example function definition follows:
(define (room-size name length width)
   (display "The ")
   (display name)
   (display " is ")
   (display length)
   (display " by ")
   (display width)
   (display " equals ")
   (display (* length width))
   (display " square feet.") )
The room-size procedure takes three arguments, the name of a room, the length of the room (in feet), and the width of the room (in feet). It prints out a sentence with the name of the room, the length, the width, and the size of the room in square feet.
Example output:
(room-size "kitchen" 12 14)
The kitchen is 12 by 14 equals 168 square feet.
;Unspecified return value
(room-size "living room" 16 24)
The living room is 16 by 24 equals 384 square feet.
;Unspecified return value
(room-size "bathroom" 8 8)
The bathroom is 8 by 8 equals 64 square feet.
;Unspecified return value
Refer to the quick reference for Scheme and Edwin for a full list of built-in Scheme functions and Edwin editor commands.
We have just seen the basics of programming languages, including how to create, use, and change data in variables, as well as how to create and use new procedures. Once you understand these concepts, you have the power to create programs. Then the real fun can begin.
Go to the index of
lectures for ECS15 - Fall 1997 .
Go to the homepage for ECS15 - Fall 1997 .
