CSC 153 Grinnell College Spring, 2009
 
Computer Science Fundamentals
 

User-Defined Procedures, Comments, and Predicates

Abstract

This lab introduces user-defined procedures (lambda expressions), the specification of comments within Scheme programs, and some Scheme predicates — procedures that return Boolean values.


Helpful Hints

Although this lab focuses on user-defined procedures, comments, and predicates, the lab begins with three useful notes.

  1. Cutting and Pasting Between Windows: In working on a workstation on the Campus Linux Network, 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 Firefox viewer.

    To select material from Firefox, 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.


Files with DrScheme

In the previous lab, you used DrScheme for simple processing. Work proceeded by typing expressions directly into the bottom interactions window of DrScheme. The Scheme environment then evaluated each expression and printed each result. Although this processing produced answers, we had to type all expressions into Scheme every time we wanted answers. As with your previous programming, a more effective approach is to store programs in a file.

In DrScheme, the large, upper area, called the definitions window, is used for creating and editing files. To get started, you can type various definitions and expressions in the top window, and these expressions can be edited as you wish.

Once you have appropriate expressions entered in the definitions window, click the "Execute" button to execute these expressions. As you will see, "Execute" clears the interactions window and executes all defined expressions. Once executed, the defined procedures from the definitions window are available in the interactions window. Thus, you can use those definitions by using them in additional expressions within the interactions window.

At any stage in typing or executing code, you can save the material in either the definitions or interactions window by choosing the appropriate option in the File menu.

Once saved, the material can be printed. Further, you can reload commands into the definitions window using the File menu.


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. To apply this function, we give it a parameter, such as 5:


    ((lambda (x) (* x x)) 5)

returns 25

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)


Comments in Scheme

As you know from your past experience, programs accomplish at least two tasks. First, computers can interpret program syntax and execute the instructions specified. Second, human beings can read the code to understand what it does and how it does it. To accomplish this second task, a programmer should include in the program explanations directed to the human reader. (These explanations also can help the programmer clarify what she or he is thinking.) While the computer ignores these explanations, they are an essential part of the program, because communicating with human readers is an essential part of the programmer's job.

A Scheme comment begins with a semicolon and extends to the end of a line. A multi-line comment can be constructed by starting each of the lines with a semicolon. For instance, the instructions in the supplemental problems for this course state that each program listing you contain comments giving your name, your mailbox number, and an identification of assignment being solved. For example:

;;; Henry M. Walker
;;; Box Science Office
;;; Supplemental Problem 2

Although one semicolon is enough to begin a comment, most Scheme programmers use several semicolons to indicate the scope or importance of a comment. A three-semicolon comment line is more likely to apply to the program as a whole, or to be extremely important; a similar line beginning with just one semicolon is likely to apply only to the line that immediately precedes or follows it and to be an off-hand remark.


Simple Equivalence

Summary: The following predicates commonly arise in Scheme:

Predicate Example that returns True (#t) Comment
number? (number? 3.1415) Is argument a number?
symbol? (symbol? 'pi) Is argument a symbol?
boolean? (boolean? #t) Is argument a boolean value?
procedure?(procedure? sqrt) Is argument a procedure?
eq? (eq? 'a 'a) Do arguments represent identical symbols?
eqv? (eqv? 'a 'a) Similar to eq?
equal? (equal? (+ 1 1) (sqrt 4)) Are arguments the same symbols, numbers, booleans, or lists?

Each of these predicates return true (#t) if the specified condition is satisfied, and false (#f) if the condition is not satisfied.


This document is available on the World Wide Web as

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

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