CSC 153: Computer Science Fundamentals Grinnell College Spring, 2005
 
Laboratory Exercise Reading
 

User-Defined Procedures

Abstract

This lab introduces user-defined procedures (lambda expressions), the Emacs editor for writing Scheme programs, and script for recording sessions.


Helpful Hints

Although this lab focuses on user-defined procedures, editing, and recording sessions, the lab begins with three useful notes.

  1. Cutting and Pasting Between Windows: In working on a MathLAN workstation, you can select and paste material from one window to another. Through this semester, this can be particularly helpful, when you want to work with material that appears on the lab directions in the Netscape viewer.

    To select material from Netscape, move the cursor to the beginning of a section and push down the left mouse button. Then, holding the button down, move the mouse to the end of the section. (The entire section now should be highlighted.) When the desired section is highlighted, stop pressing on the left mouse button -- the section should stay highlighted. Now move the mouse to where you want to paste the material, and click the middle mouse button.

  2. Arithmetic operations as an n-ary operations: In many computer languages, the usual arithmetic operations ( +, −, *, / ) are binary operations −− they apply only to 2 operands. Scheme, however, defines these operators as applying to as many operands as desired.

    For example, the volume of a sphere of radius r is given by 4/3 Pi r3. If the radius r is 5, then Scheme allows this to be computed in one statement as follows:

    (define volume (* 4/3 3.14159265358979 5 5 5))
    
  3. The Quote Procedure: Sometimes we want Scheme to print the symbol, not its value. For example, suppose we have defined the symbol pi:

    (define pi 3.14159265358979)
    

    Then we might want to print pi, not 3.14159265358979. This is done with the quote procedure:

        (quote pi)
    

    In this context, pi is returned (and printed), rather than the value that pi represents.

    (quote pi) may be abbreviated 'pi . Try typing this at the keyboard as well.


Using Emacs

Initially, you have typed commands directly into the Scheme environment, which immediately evaluated and printed the result. While this use of Scheme is quite straightforward, it has at least two disadvantages. First, all program definitions must be retyped every time a program is to be run, as nothing is saved from one use of Scheme to the next. Second, the results of a computation are not saved, and thus they are difficult to print. This discussion addresses both of these issues.

  1. Creating New Scheme Files: We will use the Emacs editor to create a file for a Scheme program. This is done by clicking with the left mouse button on the icon with the head of a gnu (an African antelope) at the bottom of the screen. Shortly, a new window will appear for your use in entering your program.

    Often, it is convenient to move this window to the right the computer screen. This is accomplished by moving the mouse to the labeled bar emacs:... at the top of the Emacs window. Press the left mouse button on this labeled bar, and move the mouse (keeping the left button depressed). The Emacs window will follow your mouse movements. When the Emacs window is where you want it, release the left mouse button.

    While Emacs is an extremely powerful editor, many common capabilities are highlighted with buttons and menus at the top of window. These menus are analogous to most word processing packages, and thus are not discussed here. Ask the instructor as questions arise.

    If something particularly strange seems to be happening in Emacs, type <Ctrl/g> to stop the processing of a command.

  2. To run a program for now, you should open a terminal window. As in the previous lab, this may be done by clicking on the icon of a computer screen and keyboard. This time, you may want to move the terminal window to the lower-left of the screen, so you can see much of both the Emacs and terminal windows at the same time. (Firefox may be placed at the upper-left of the screen.)

    Work now involves three components:

    For much processing in this course, you will move among these windows, copying material from a Firefox window, pasting material into Emacs, editing Emacs, and loading the result into Scheme with the load procedure.


Lambda Expressions

A lambda expression is a way of defining a mathematical function in Scheme. For example, the squaring function could be written:
    (lambda (x) (* x x))
This expression indicates that the input parameter is x and the result of the function will be computed as (* x x).

In Scheme, a lambda expression is called a procedure.

Naming Procedures: As the previous example shows, lambda expressions operate on the expression that follows them. However, in the example, the lambda expression was defined only for a single expression, and we would have to type the lambda expression each time we used it. Since this is inconvenient, we often want to give the procedure a name, which we then can use repeatedly. More formally, we use define to bind a lambda expression to a symbol. If we call the above function f, then the above computations could be done as follows:

    (define f
        (lambda (x) (* x x)))
    (f 1)
    (f 3)
    (f -2)


Recording a Session:

Your activity at a workstation may be recorded in a file using the following steps.


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/153.sp05/readings/reading-procedures.shtml

created January 22, 1977
last revised January 25, 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.