Fundamentals of CS I (CS151 2002F)

Procedure Definitions

The Problem: Naming Expressions

In previous labs, you've seen that it's possible to define complex expressions from simpler expressions. For example, we might write the following for express val explicitly as a rational number.

(/ (inexact->exact (numerator val))
   (inexact->exact (denominator val)))

What happens when we want to try this expression using different values? One possibility is to redefine grade and then reexecute the expression.

> (define val (sqrt 2))
> (/ (inexact->exact (numerator val))
     (inexact->exact (denominator val)))
1592262918131443/1125899906842624
> (define val 5)
> (/ (inexact->exact (numerator val))
     (inexact->exact (denominator val)))
5
> (define val (exp 1))
765128314358509/281474976710656

But this seems cumbersome. It would be nice to simply give a name to this expression and use that name. Unfortunately, our current way of naming doesn't quite work.

> (define val (sqrt 2))
> (define frac (/ (inexact->exact (numerator val))
                   (inexact->exact (denominator val)))
> frac
1592262918131443/1125899906842624
> (define val 5)
> frac
1592262918131443/1125899906842624

What's going on? Scheme evaluated the expression that accompnaies frac once, when it was first defined. Hence, since the expression had a particular value once, it retains that value forever.

What we'd really like to do is to say that frac is a procedure that takes a value as an input and returns an appropriate fraction. You know that Scheme has procedures, since you've used lots of built-in procedures, including sqrt, *, cons, and list. But can you define your own procedures? Yes.

Defining Procedures

You use define to give names to procedures, just as you use it to give names for values. The values just look different. The general form of a procedure is

(lambda (formal-parameters)
  expression)

For example, we might write our frac procedure as

(define frac
  (lambda (val)
     (/ (inexact->exact (numerator val))
        (inexact->exact (denominator val)))))

Our frac procedure can now be called as if it were a built-in procedure.

> (frac (sqrt 2))
1592262918131443/1125899906842624
> (frac 5)
5
> (frac 22/7)
22/7

We can define procedures for anything we already know how to do in Scheme. For example, here is a simple square procedure.

(define square
  (lambda (n)
    (* n n)))

We can test it.

> (square 2)
4
> (square -4)
16
> (square square)
*: expects type <number> as 1st argument, given: #<procedure:square>; other arguments were: #<procedure:square>
> (square 'a)
*: expects type <number> as 1st argument, given: a; other arguments were: a

Documenting Your Procedures

Convention in Scheme (and all programming languages) is that we carefully document what our procedures do, including input values, output values, and assumptions. We use comments provide information to the reader of our program (that is, to people instead of the computer). In Scheme, comments begin with a semicolon and end with the end of the line.

;;; Samuel A. Rebelsky
;;; Department of Mathematics and Computer Science
;;; Grinnell College
;;; rebelsky@cs.grinnell.edu

;;; Procedue:
;;;   square
;;; Parameters:
;;;   val, a number
;;; Purpose:
;;;   Compute val*val
;;; Produces:
;;;   The result of the computation
;;; Preconditions:
;;;   val must be a number
;;; Postconditions:
;;;   The result is the same "type" of number as val (e.g., if
;;;   val is an integer, so is the result; if val is exact,
;;;   so is the result).
;;; Citations:
;;;   Based on code created by John David Stone dated March 17, 2000
;;;   and contained in the Web page 
;;;   http://www.math.grin.edu/~stone/courses/scheme/procedure-definitions.xhtml
;;;   Changes to
;;;     Parameter names
;;;     Formatting
;;;     Comments
(define square
  (lambda (value)
    (* value value)))

Yes, that's a lot of documentation for very little code. However, it is better to err on the side of too much documentation than too little documentation. More importantly, as you start writing more procedures, their purpose and details will be much less obvious.

Procedures with More Than One Parameter

At times, we'll want to write procedures that take more than one parameter. Such procedures look just like procedures with one parameter, except that you can list more parameters between the parentheses.

(lambda (param1, param2 ... paramn)
  expression
)

For example, here is a simple procedure that finds the average of two nubmers

;;; Samuel A. Rebelsky
;;; Department of Mathematics and Computer Science
;;; Grinnell College
;;; rebelsky@cs.grinnell.edu

;;; Procedure:
;;;   pairave
;;; Parameters:
;;;   val1, an exact number
;;;   val2, an exact number
;;; Purpose:
;;;   Compute the average of two numbers.
;;; Produces:
;;;   ave, The average of those two numbers.
;;; Preconditions:
;;;   Both val1 and val2 are exact numbers.
;;; Postconditions:
;;;   ave is an exact number.  
;;;   ave is equidistant from val1 and val2.  That is 
;;;     (abs (- val1 ave)) equals (abs (- val2 ave))
(define pairave
  (lambda (val1 val2)
    (/ (+ val1 val2) 2)))

As this example may suggest, in your documentation it is particularly important to think about what you can guarantee about the results of your procedure. That is, what does it mean to be the average of two values.

 

History

Monday, 4 September 2000 [Samuel A. Rebelsky]

Wednesday, 31 January 2001 [Samuel A. Rebelsky]

Monday, 9 September 2002 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Dec 2 08:42:07 2002.
The source to the document was last modified on Mon Sep 9 08:35:01 2002.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Readings/procedures.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu